To store a series of data, using arrays is a good choice. arrays are composed of a group of variables of the same data type, which are represented by a common name, the individual elements in the array are marked to indicate their locations. Arrays can be divided into one-dimensional arrays, two-dimensional arrays, and multi-dimensional arrays according to the complexity of storing elements.
An array is a set of ordered data. Each element in an array has the same data type. You can use a uniform name and subscript to uniquely determine the elements in the array.
Declaration of one-dimensional array and Memory Allocation
To use an array in JAVA, follow these two steps:
(1) Declare the name of the array data type array [];
(2) allocate memory to this array name = new data type [number]
In the declaration format of an array, the data type refers to the data type of an array element. Common types include integer, floating point, and complex. The array name is used to unify the names of the original acid groups of the same data type. The naming rules are the same as the variables. The number indicates how many elements should be stored in the array declared by the compiler, the 'new' keyword is that the compiler opens a memory block in the memory according to the number for this array. For example:
Int score []; // declares an integer array score. It can also be understood as an array type variable named score [], which does not contain any content, the compiler allocates only one piece of memory to it, and the memory size is unknown.
Score = new int [3]; // allocates memory space for the score in an integer array. The number of elements is 3, that is, three data storage spaces are allocated to the memory, in fact, when declaring an array, you can assign the memory space to it at one time, that is, int score [] = new int [11]; that is, it declares an integer array score with 11 elements, at the same time, a piece of memory is opened for its use.
How to save elements in an array:
To use the elements in the array, you can use indexes. In Java, the array index number starts from 0. In the above example, the first element is score [0], and the second element is score [1]. Function for obtaining the array length: array name. length
Output the content in the array. You can use the for loop.
[Java]
Package hello.csdn.com;
Public class HelloWorld {
Public static void main (String [] args ){
Int score [] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
For (int I = 0; I <score. length; I ++ ){
System. out. println ("score [" + I + "] =" + score [I]);
}
}
}
In the above example, the initial value is assigned to the array when the array is declared. The method for assigning the initial value to the array is:
Data Type array name [] = {initial value 0, initial value 1, initial value 2 ......};
Array application example:
[Java]
Package hello.csdn.com;
Public class HelloWorld {
Public static void main (String [] args ){
Int score [] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Int I, max, min;
Max = min = score [0];
For (I = 0; I <score. length; I ++ ){
If (score [I]> max ){
Max = score [I];
}
If (score [I] <min ){
Min = score [I];
}
} Www.2cto.com
System. out. println ("the maximum value in the array is" + max );
System. out. println ("the minimum value in the array is" + min );
}
}
Array-related API functions