不得建立新的数组
package Demo2;
public class diandao {
public static void main(String[] args) {
//将列表中的元素颠倒
int[] array1 = {4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < array1.length; i++) {
int left = i;
int reight = array1.length - 1 - left;
if(left < reight){
int temp = array1[i];
array1[i] = array1[reight];
array1[reight] = temp;
}
}
System.out.println("---------------");
for (int i = 0; i < array1.length; i++) {
System.out.println(array1[i]);
}
}
}
结果:
—————
10
9
8
7
6
5
4
如有失效,请留言告知丨转载请注明原文链接:Java中将数组中的元素颠倒