int arrInt[] 或者 int[] arrInt
int[] arrInt = new int[4];
int[] arrInt = {1,3,5,7}; 或 int[] arrInt = new int[4]; arrInt[0] = 1; arrInt[1] = 3; arrInt[2] = 5; arrInt[3] = 7;
示意圖以下:java
int[][] arrInt = {{1,3,5},{2,4,6},{0,10,20}};
示意圖以下:數組
System.out.println("int array:" + new int[3].getClass()); System.out.println("int array:" + new int[3].getClass().getSuperclass()); System.out.println("String array:" + new String[3].getClass()); System.out.println("boolean array:" + new boolean[3].getClass());
int array:class [I int array super:class java.lang.Object String array:class [Ljava.lang.String; boolean array:class [Z
int arrInt[] = {1,2,3}; int arrClone[] = arrInt.clone(); arrClone[1] = 5; System.out.println(arrInt == arrClone); for (int i = 0; i < arrClone.length; i++) { System.out.println(arrInt[i]+" "+arrClone[i]); }
false
1 1
2 5
3 3
示意圖以下: 數據結構
int arrInt[][] = {{1,2,3},{4,5,6}}; int[][] arrClone = arrInt.clone(); arrClone[0][1] = 50; arrInt[0][1] = 100; System.out.println(arrInt == arrClone); System.out.println(arrInt[0] == arrClone[0]); for (int i = 0; i < arrInt.length; i++) { for (int j = 0; j < arrInt[i].length; j++) { System.out.println(arrInt[i][j]+" "+arrClone[i][j]); } }
false true 1 1 100 100 3 3 4 4 5 5 6 6
示意圖以下:spa
private int[] insertArr(int[] srcArr, int position, int value) { if (position < 0 || position >= srcArr.length) return null; int[] desArr = new int[srcArr.length+1]; System.arraycopy(srcArr, 0, desArr, 0, position); desArr[position] = value; System.arraycopy(srcArr, position, desArr, position+1, srcArr.length-position); return desArr; }
最後整理個表格,上述操做的時間複雜度大體以下,很容易理解就不詳細解釋了。code
操做
|
平均時間複雜度
|
最壞條件時間複雜度
|
插入
|
O(n)
|
O(n)
|
刪除
|
O(n)
|
O(n)
|
查找
|
O(n)
|
O(n)
|
訪問
|
O(1)
|
O(1)
|