public class d9 {
public static void main(String[] args) {
int[] arr = {19, 28, 37, 46, 50};
//循环遍历数组
/*for (int start=0,end=arr.length-1;start<=end;start++,end--){
//变量交换
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;*/
}
reverse(arr);
//遍历数组
printarray(arr);
}
public static void reverse(int[] arr) {
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
//变量交换
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
public static void printarray(int[] arr){
System.out.print("[");
for(int x=0;x< arr.length;x++){
if (x== arr.length-1){
System.out.print(arr[x]);
}else{
System.out.print(arr[x] + ",");
}
}
System.out.print("]");
}
}
求解怎么改
public static void main(String[] args) {
int[] i=new int[]{1,2,3,4,5};
reverse(i,0,5);
System.out.println(JSON.toJSONString(i));
}
public static void reverse(final int[] array, final int startIndexInclusive, final int endIndexExclusive) {
if (array == null) {
return;
}
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
int tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
这写的没问题么,反转不了吗,还是你要的效果不是这个?
jdk8
public static void main(String[] args) {
int[] arr = {19, 28, 37, 46, 50};
List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
Collections.reverse(list);
System.out.println(list);
}