An array is a compound data type in order of variables of the same type, called variables of the same type as elements or cells of the array. Arrays are Gazzo by array names to draw elements that use arrays.
An array is a reference variable, and creating an array requires two steps to declare an array and assign variables to arrays.
Declaring an array
declaring an array includes the name of an array variable (For short, array name ) , the type of the array.
Declares a one-dimensional array of the following two formats
the element type of the array Array name [];
the element type of the array [] array name ;
For example: float boy[];
Float[] Boy;
Declares a two-dimensional array of the following two formats
the element type of the array Array name [][];
the element type of the array [] [] array name ;
Example: char cat[][];
Char[][] Cat;
assigning variables
After declaring an array, you must also allocate memory space for it, and the format of allocating memory space for one-dimensional arrays is as follows:
array Name = new array element type [ number of array elements ];
For example: float boy[];
boy= New Float[4];
Declaring an array and creating an array can be done together, for example:
float boy[]=new Float[4];
This is a boy of length 4 . Array type
a two-dimensional array, like a one-dimensional array, must be assigned a space within the array with the new operator after the declaration.
Example: int mytwo[][];
mytwo = new int [3][4];
Declaring an array and creating an array can be done together, for example:
int mytwo[][] = new INT[3][4];
Java uses an array of arrays to declare a multidimensional array. The two-dimensional array created above is Mytwo by 3 One-dimensional arrays of length 4 :mytwo[0] ,mytwo[1] and mytwo[2] .
Working with arrays
a one-dimensional array accesses its own elements, such as boy[0],boy[1 , etc. through an index . It is important to note that the index starts at 0 .
a two-dimensional array also accesses its own elements through an index, such as a[0][1],a[1][2] , and so on. It is important to note that the index starts at 0 .
For example, a declaration creates a two-dimensional array a:
int a[][] = new int[2][3];
then the first index changes from 0 to 1, and the second index varies from 0 to 2.
Use of Length
The number of elements in an array is called the length of the array.
for a one-dimensional array,the value of " array name . Length" is the number of elements in the array.
The value of " array name . Length" for a two-dimensional array is the number of one-dimensional arrays it contains.
For example, for
float a[] = new FLOAT[12];
int b[][] = new INT[3][6];
The value of A.length is 3,while The value of B.length is.
Initialization of the array
after the array is created, the system gives a default value to each element of the array, such as thefloat type is 0.0.
When declaring an array, you can also give an initial value to the elements of the array, such as:
Float boy[] = {21.3f,23.89f,2.0f,23f,778.98f};
You can also initialize a two-dimensional array directly with several one-dimensional arrays, and the lengths of these one-dimensional arrays are different.
For example:
int a[][]= {{1}, {1,1},{1,2,1}, {1,3,3,1},{1,4,6,4,1}};
References to arrays
Arrays are reference variables, so two arrays of the same type have exactly the same elements if they have the same reference.
For example, for int a[] = {b[},]= {4,5}; Array Variables a and the b the references are stored separately 0x35ce36 and the 0X757AEF .
if the following assignment statements are used (The type of a and b must be the same ) a=b; So, a and the references stored in the b the system will release the original assigned to the array a the elements that make a the Elements and b the same elements.
Traversal of an array
based on the traversal of the loop statement , the syntax is as follows:
For ( declares the loop variable: the name of the array ) {
... ...
}
Where the type of the declared loop variable must be the same as the type of the array.
You can translate this form of A for statement into " the value of each element of an array in turn for a loop variable ."
Example:
public class demo11{
public static void Main (String args[]) {
int a[] = {1,2,3,4}; /* Create an array of type int
System.out.println (" number of array elements:" +a.length);
System.out.println ("a[1]:" +a[1]); /* use subscript to access specific array elements
for (int i=0;i<a.length;i++) {/* after execution, each element in the array is traversed to
System.out.println ("a[" +i+ "]:" +a[i]);
}
}
}
Test array
Traverse
original link:http://www.maiziedu.com/wiki/java/array/
The Java array is detailed