声明数组变量
public class ArrayTest { public static void main(String[] args) { int[] array1 = new int[5]; int[] array2; array2 = new int[5]; int[] array3 = {1,2,3,4,5}; System.out.print("array1:"); for (int i = 0; i < 5; i ++) System.out.print(array1[i] + " "); System.out.printf("/narray2:"); for (int i = 0; i < array2.length; i ++) System.out.print(array2[i] + " "); System.out.printf("/narray3:"); for (int arr : array3) System.out.print(arr + " ");//JDK5.0 System.out.println(); } } |
输出结果:
array1:0 0 0 0 0
array2:0 0 0 0 0
array3:1 2 3 4 5
动态申请数组长度
import java.util.Scanner;
public class ArrayLength { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Please input the length:"); int length = scanner.nextInt();
int[] array = new int[length];
for (int arr : array) System.out.print(arr + " "); System.out.println(); } } |
输出结果:
Please input the length:10
0 0 0 0 0 0 0 0 0 0
二维数组
public class TwoDimArray { public static void main(String[] args) { int[][] array1 = { {1,2,3},{4,5,6} }; int[][] array2 = new int[3][4];
for (int i = 0; i < array1.length; i ++) for (int j = 0; j < array1[i].length; j ++) System.out.print(array1[i][j] + " "); System.out.println(); for (int[] row : array2) for (int b : row) System.out.print(b + " "); System.out.println(); } } |
不规则数组
public class LotteryArray { public static void main(String[] args) { int[][] array1 = new int[2][]; array1[0] = new int[5]; array1[1] = new int[10];
for (int i = 0; i < array1.length; i ++) { for (int j = 0; j < array1[i].length; j ++) System.out.print(array1[i][j] + " "); System.out.println(); } } } |
输出结果;
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
public class LotteryArrayTest { public static void main(String[] args) { final int MAX = 10;
int[][] odds = new int[MAX+1][]; for (int i = 0; i <= MAX; i ++) odds[i] = new int[i+1];
for (int i = 0; i < odds.length; i ++) for (int j = 0; j < odds[i].length; j ++) { int lotteryodds = 1; for (int k = 1; k <= j; k ++) lotteryodds = lotteryodds *(i-k+1)/k;
odds[i][j] = lotteryodds; } for (int[] row : odds) { for (int odd : row) System.out.printf("%4d",odd); System.out.println(); } } } |
输出结果:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
数组进阶
public class AdvanceArray { public static void main(String[] args) { int[] array = new int[]{1,2,3,4,5}; int[] temp; temp = array; for (int a : array) System.out.print(a + " "); System.out.println(); for (int t : temp) System.out.print(t + " "); System.out.println(); temp[3] = 0; for (int a : array) System.out.print(a + " "); System.out.println(); for (int t : temp) System.out.print(t + " "); System.out.println(); } |
输出结果:
1 2 3 4 5
1 2 3 4 5
1 2 3 0 5
1 2 3 0 5
array和temp引用自同一对象,对两者的操作都会改变其指向的值。
数组拷贝
System.arraycopy(from, fromIndex, to, toIndex, count);
将第一个数组的第fromIndex个位置开始拷贝count个元素,目标数组
起始位置为toIndex
System.arraycopy(来源, 起始索引, 目的, 起始索引, 复制长度);
public class CopyArray { public static void main(String[] args) { int[] array1 = new int[]{1,2,3,4,5}; int[] array2 = new int[5]; int[] array3 = new int[5]; int[] array4 = new int[] {11,12,13,14,15}; for (int i = 0; i < array1.length; i ++) array2[i] = array1[i];
System.arraycopy(array1, 0, array3, 0, array1.length); System.arraycopy(array1, 1, array4, 2, 3); for (int arr : array2) System.out.print(arr + " "); System.out.println(); for (int arr : array3) System.out.print(arr + " "); System.out.println(); for (int arr : array4) System.out.print(arr + " "); } } |
输出结果:
1 2 3 4 5
1 2 3 4 5
11 12 2 3 4