An array is a combination of multiple arrays of the same data type, where the data type is no matter what the data type.
Array variables are reference-type variables, arrays can be objects, and each element in an array is equivalent to a member variable of an object, so array elements can be initialized by default. (Blog java--Variable Classification There is a description of the initialization of member variables).
one-dimensional arrays
declaration
type var[]; or type[] var< /span>
initialize
int a[];a=new int[3];a[0]=0;a[1]=1;a[2]=2;
Static initialization: Defines the same time that the array element allocates space and assigns a value
int a[]={1,2,3};
memory analysis
Class Testarray{public static void Main (String arg[]) {int[] s;s=new int[5];for (int i=0;i<5;i++) {s[i]=i;}}}
The above program runs to the last. The memory should be this:
two-dimensional arrays
two-dimensional arrays can be considered as elements of an array The array.
initialize
Dynamic initialization (note: java the Declaration and creation of multidimensional arrays in the order from high-to low-dimensional )
int a[][]=new int[3][];a[0]=new int[2];a[1]=new int[4];a[2]=new int
[3];
int a[][]={{1,2},{2,3},{3,4}};
Memory analysis:
Class Testarr{public static void Main (String arg[]) {int s[][]=new int[3][];s[0]=new int[2];s[1]=new int[4];s[2]=new int[ 3];for (int i=0;i<a.length;i++) {for (int j=0;j<s[i].length;j++) {s[i][j]=i;}}}}
The above program runs to the end, the memory should be this
Summary
The element of a one-dimensional array in 1.java is the same variable as the data type, while a two-dimensional array is an array of multiple one-dimensional array groups, so it is important to understand one dimension well. 2. An array is a reference variable that can be used as an object. And its element is the member variable.
java--Array