reverse array in java
int length = array.length;
for(int i=0;i<length/2;i++) {
int swap = array[i];
array[i] = array[length-i-1];
array[length-i-1] = swap;
}
or
Collections.reverse(Arrays.asList(array));
reverse array in java
int length = array.length;
for(int i=0;i<length/2;i++) {
int swap = array[i];
array[i] = array[length-i-1];
array[length-i-1] = swap;
}
or
Collections.reverse(Arrays.asList(array));
java sort array descending
Integer[] cubes = new Integer[] { 8, 27, 64, 125, 256 };
Arrays.sort(cubes, Collections.reverseOrder());
Read more: https://www.java67.com/2016/07/how-to-sort-array-in-descending-order-in-java.html#ixzz6MX60xZLb
sorting array in descending order java
// A sample Java program to sort a subarray
// in descending order using Arrays.sort().
import java.util.Arrays;
import java.util.Collections;
public class SortExample
{
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = {13, 7, 6, 45, 21, 9, 2, 100};
// Sorts arr[] in descending order
Arrays.sort(arr, Collections.reverseOrder());
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us