An array is a collection of elements of the same data type that itself is a reference data type, that is, an object. But arrays can store basic data types, or they can store reference data types.
In Java, if you want to save a set of basic types of data, use an array, or use a collection if you want to save a set of objects or other types of complex data.
Examples of arrays
int [] A = new int []{1,2,3,4,5};
string [] s = new string []{"Bear", "Little Bear", "Little Bear"};
Employee [] E=new employee[10]; (Employee is a custom class)
Three ways to create an array:
Allocates memory based on the specified length while declaring an array, but the element values in the array are the default initialization values
char[] Chary = new CHAR[10];
int[] Arrayd = new int[2]{1,2}; incorrect initialization, cannot define both the initialization value of the peace
Declares an array and allocates memory, which is initialized at the same time
int[] Ary1 = new Int[]{1, 2, 3, 4, 5};
The same way as the previous one, just a relatively brief syntax
Int[] Ary2 = {1, 2, 3, 4, 5};
From another angle, array creation can be divided into two types, dynamic and static
Dynamically create an array (no value assigned to the element, can be assigned with a For loop) char[] chary = new CHAR[10];
Statically creates an array, at the time of creation, assigns an initial value to each element int[] Ary1 = new Int[]{1, 2, 3, 4, 5};
The length of the array
Length of array: Long property
int [] B1 = new int []{1,2,3,4,5,6,7};
System.out.println (b1.length);
Note: The length of the array is an attribute, and the length of the string is the long () method;
Note the String.Length () is distinguished from the string type. Array uses the way member variables are, and string uses the method of member methods.
At the same time, Array.Length can only get the size of the array, but not how many elements the array actually has. The length of a multidimensional array is calculated only for the first dimension.
When creating an array, you must specify the length of the array, and no change is allowed once defined.
The length of the array, although 7, is actually given in memory to 8 locations, and the other one stores 7.
assigning values between arrays
In the C language, you cannot assign an array directly to another array;
In Java, this is syntactically permissible, but the actual effect is that two array references point to the same block of memory.
Int[] Ary1 = {2, 4, 6, 8, 10};
Int[] Ary2;
Ary2 = Ary1; Allowed to assign this value
ARY2[3] = 1024; Modify the value of one of the elements through array 2
access to array elements : array name [index]
Example: A[2]; Note: The index of the array starts at 0.
The data type of an index is an integer index maximum and array length is always 1
Definition of a two-dimensional array:
A multidimensional array is an array of arrays, that is, an array of elements.
For example: int [][]a = {{1},{1,2},{1,2,3}};
Declaration of a two-dimensional array: three different ways
int [] [] A;
int []a1[];
int a2[][]; Note: The first type of data that is not easily confused with a is recommended
Iterations of multidimensional arrays:
int [] [] B1 = new int [][]{{1,2},{3,4}};
for (int i =0;i<b1.length;i++) {
for (int J =0;j<b1[i].length;j++) {
System.out.print (B1[i][j]);
}
}
Java Base Syntax array