Knowledge points: 1. Definition and initialization of arrays 2. One-dimensional arrays and multidimensional arrays one, array
First, let's take a look at a picture, which is introduced to the web.
Array, we can get the value of the element by subscript, it is worth noting that after learning the programming, the number starts from 0,
That is, I said, our subscript is also starting from 0, to the end of the array-1 ends.
For example the above picture wants to get 10, that is ages[0] can get to 10.
1. Syntax
data type [] array name; Or: Data type array name [];
Where the array name can be any valid variable name, such as:
int ages[]; An array of age, type Integer char symbol[]; The array that holds the symbol, the type is the character string [] name; An array of names that are string-type
When we declare an array, we want to allocate space for arrays, that is, how large arrays are defined.
Grammar:
Array name = new data type [array length];
The array length is the maximum number of elements the array can hold. We can also initialize an array when the array is declared, or allocate a space for it at the time of declaration, so that we can no longer allocate space for arrays.
Grammar:
int [] ages = {12,18,9,33,45,60}; Declares and initializes an array of integers with 6 elements char [] symbol = new CHAR[10]//declares and assigns a char array of length 10
In Java, you can assign an array to another array, such as:
int [] A1 = {1,2,3};int [] a2;a2 = A1;
One-dimensional arrays we can iterate by simply looping.
For example, we want to output the array ages we defined above on the console.
int [] ages = {N, 9, 0, $, 60};for (int, i < ages.length; i++) {//ages.length is the length of the Get array System.out.printl N (the "+ (i+1) +" element in the array is "+ages[i"); Array subscript is zero-based, be sure to note}
Operation Result:
Note: In addition to the above subscript is starting from 0, there are also arrays can not cross-border access, or to error.
Two or two-dimensional array 1. One-dimensional array insert: Foreach Loop
The For statement can use a special simplified version within an array, which is simpler and easier to iterate through arrays and collections. To understand the meaning of foreach as "for each" from the literal meaning of English.
Grammar:
for (element type element variable: Traverse object) { executed code}
For example, the above example:
public class Judgeprime {public static void Main (string[] args) { int [] ages = {N, 9, +, A, a, $}; int i = 1; The for (int age:ages) { System.out.println ("+i+" element in the array is "+age"); i++;}}}
2. Two-dimensional array
In our programming career we also encounter many multidimensional array applications in addition to one-dimensional arrays, where two-dimensional arrays are the most common.
Two-dimensional array can be regarded as a classroom with a seat, we usually use the first few rows of the number of positioning, each seat has a row and a column of properties, a row of seats equivalent to a one-dimensional array, so we can simply understand the two-dimensional array is a "special" one-dimensional array, Each of its array spaces is saved in a one-dimensional array.
Two-dimensional arrays also need to declare and allocate space.
Grammar:
data type [] Array name = new data type [number of rows] [column number];//or data type [] array name; array name = new data type [number of rows] [number of columns];//can also data type [] Array name = {{First row value 1, first row value 2,...} {Second row value 1, second row value 2,...} ...} The assignment and access of two-dimensional arrays, similar to one-dimensional arrays, can be assigned and accessed by subscript, paying attention to the index starting from 0 array name [index of row] = value;
Example:
String [[] name = {{"Zhaoyi", "Qianer", "Sunsan"}, {"LiSi", "Zhouwu", "Wuliu"}}; for (int i = 0, i < 2; i++) {for (int j = 0; J < 3; J + +) { System.out.println (name[i][j]);} }
Operation Result:
"Java Base-java array"