2017-07-06 327 views

回答

2

int a[] = {1,9}是兩個元件1和9 int[] a被聲明一個整數數組稱爲陣列。未初始化。 int a[9]是一個包含九個元素的數組。未初始化 int[0]=10地方值10在位置0處的陣列

+2

'int a [9];'是非法的 –

+0

正確並被刪除。 – MaxPower

4

考慮以下方案

// Create new array with the following two values 
int a[] = {1,9}; 
Assert.assertTrue(2 == a.length); 
Assert.assertTrue(1 == a[0]); 
Assert.assertTrue(9 == a[1]); 

// Create a new, uninitialized array 
int[] a; 
Assert.assertTrue(null == a); 
Assert.assertTrue(0 == a.length); // NullPointerException 

int a[9]; // this will not compile, should be 
int a[] = new int[9]; 
Assert.assertTrue(9 == a.length); 
Assert.assertTrue(null == a[0]); 

// Provided 'a' has not been previously defined 
int a[]; 
a[0] = 10; // NullPointerExcpetion 

// Provided 'a' has been defined as having 0 indicies 
int a[] = new int[0]; 
a[0] = 10; // IndexOutOfBoundsException 

// Provided 'a' has been defined as an empty array 
int a[] = new int[1]; 
a[0] = 10; // Reassign index 0, to value 10. 
+0

「創建一個新的,未初始化的數組」僅當'int [] a'是一個類變量時。如果它是一個局部變量,你會得到一個錯誤,因爲它沒有被分配。 –

+1

「'null == a [0]'」不會編譯,因爲'a [0]'是一個'int'。 –

0

int arr1[] = new int[10];上 - >大小爲10的一個空數組(存儲器分配給它)

int a[] ={1,9}; - > [1,9](創建值爲1和9的數組)

int[] a ; - >已聲明但未初始化的數組

int a[9]; - >不工作

參見Arrays

相關問題