声明数组:
方式一:
int[] a = new int[6];
方式二:
int[] b = new int[]{1, 2, 3, 4, 5, 6};
方式三:
int[] c = {1, 2, 3, 4, 5, 6};
数组倒置:
方式一:
public static void main(String[] args) { int[] b = new int[]{1, 2, 3, 4, 5, 6}; // 倒置 b数组 for (int i = 0; i < b.length / 2; i++) { // 交换 int temp = b[i]; b[i] = b[b.length - 1 - i]; b[b.length - 1 - i] = temp; } // 遍历 System.out.println(Arrays.toString(a)); }
方式二:
public static void main(String[] args) { int[] a = {1, 2, 3, 4, 5, 6}; for (int i = 0, j = a.length - 1; i <= j; i++, j--) { // 交换 a[i] = a[i] + a[j] - (a[j] = a[i]); } System.out.println(Arrays.toString(a)); }