2017-07-06 328 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

相关问题