public class ArrayHandle {
public static Object[] reverse(Object[] arr) {
List<Object> list = Arrays.asList(arr);
Collections.reverse(list);
return list.toArray();
}
}
public static void reverse(int[] data) {
for (int left = 0, right = data.length - 1; left < right; left++, right--) {
// swap the values at the left and right indices
int temp = data[left];
data[left] = data[right];
data[right] = temp;
}
}
我还认为在while循环中执行这个操作更具可读性。
public static void reverse(int[] data) {
int left = 0;
int right = data.length - 1;
while( left < right ) {
// swap the values at the left and right indices
int temp = data[left];
data[left] = data[right];
data[right] = temp;
// move the left and right index pointers in toward the center
left++;
right--;
}
}
public void display(){
String x[]=new String [5];
for(int i = 4 ; i > = 0 ; i-- ){//runs backwards
//i is the nums running backwards therefore its printing from
//highest element to the lowest(ie the back of the array to the front) as i decrements
System.out.println(x[i]);
}
}
public static void main (String args[]){
//create array
String[] stuff ={"eggs","lasers","hats","pie","apples"};
//print out array
for(String x :stuff)
System.out.printf("%s ", x);
System.out.println();
//print out array in reverse order
for(int i=stuff.length-1; i >= 0; i--)
System.out.printf("%s ",stuff[i]);
}
public class ReverseArray {
public static void main(String[] args) {
int arr[] = new int[] { 10,20,30,50,70 };
System.out.println("reversing an array:");
for(int i = 0; i < arr.length / 2; i++){
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
public final class ReverseComparator<T extends Comparable<T>> implements Comparator<T> {
@Override
public int compare(T o1, T o2) {
return o2.compareTo(o1);
}
}
一个简单的
Integer[] a = {1,6,23,4,6,8,2}
Arrays.sort(a, new ReverseComparator<Integer>());
public void getDSCSort(int[] data){
for (int left = 0, right = data.length - 1; left < right; left++, right--){
// swap the values at the left and right indices
int temp = data[left];
data[left] = data[right];
data[right] = temp;
}
}
public static int []reversing(int[] array){
int arraysize = array.length;
int[] reverse = new int [arraysize+1];
for(int i=1; i <= arraysize ; i++){
int dec= arraysize -i;
reverse[i] = array[dec];
}
return reverse;
}
List<Integer> normalArray= new ArrayList<>();
List<Integer> reversedArray = new ArrayList<>();
// Fill up array here
for (int i = 1; i <= normalArray.size(); i++) {
reversedArray .add(normalArray.get(normalArray.size()-i));
}
public static int[] reverse(int[] array) {
int j = array.length-1;
// swap the values at the left and right indices //////
for(int i=0; i<=j; i++)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
j--;
}
return array;
}
public static void main(String []args){
int[] data = {1,2,3,4,5,6,7,8,9};
reverse(data);
}
int[] a = {1,2,3,4,5,6,7,23,9}, b; //compound declaration
var j = a.length;
b = new int[j];
for (var i : a)
b[--j] = i; //--j so you don't have to subtract 1 from j. Otherwise you would get ArrayIndexOutOfBoundsException;
System.out.println(Arrays.toString(b));
//Reverse and get new Array -preferred
public static final <T> T[] reverse(final T[] array) {
final int len = array.length;
final T[] reverse = (T[]) Array.newInstance(array.getClass().getComponentType(), len);
for (int i = 0; i < len; i++) {
reverse[i] = array[len-(i+1)];
}
return reverse;
}
//Reverse existing array - don't have to return it
public static final <T> T[] reverseExisting(final T[] array) {
final int len = array.length;
for (int i = 0; i < len/2; i++) {
final T temp = array[i];
array[i] = array[len-(i+1)];
array[len-(i+1)] = temp;
}
return array;
}