04Java Basic _ Array
Array-related concepts
VIn Java, arrays are composite data types (reference data types ). An array is a set of ordered data. Each element in an array has the same data type. You can use a unified array name and subscript to uniquely identify the elements in the array. Arrays include one-dimensional arrays and multi-dimensional arrays.
VAn array in Java is an object.
Definition of one-dimensional array
VThe definition of a one-dimensional array is as follows:
§ Type arrayname []; or type [] arrayname;
§ Type can be any data type in Java, including simple type and combination type. The array name arrayname is a legal identifier, [] indicates that the variable is an array type variable.
Java does not allocate memory for array elements in the array definition. Therefore, [] does not need to specify the number of elements in the array, that is, the length of the array, in addition, an array defined above cannot access any of its elements. The elements of the array can be applied only after initialization.
Initialize a one-dimensional array (static)
VAfter a one-dimensional array is defined, it can be referenced only after initialization. Array initialization includes static initialization and dynamic initialization;
VStatic initialization: initializes array elements while defining arrays.
Dynamic initialization: use the new operator to allocate space for arrays. For arrays of simple types,
Notes for one-dimensional array Initialization
VOne-dimensional arrays are dynamically initialized. If only new (only memory space is allocated) is not assigned a value, all elements in the integer array are 0, and all elements in the floating point array are 0.0; all elements in the Boolean array are false, and all elements in the array of the class type are null;
Use of one-dimensional arrays
VWhen an array is defined and the memory space is allocated with the new operator, each element in the array can be referenced. Element reference method: arrayname [Index]
VIn addition, Java checks the array elements out of bounds to ensure security. At the same time, each array has a property length to specify its length.
Multi-dimensional array
Multi-dimensional arrays are considered as arrays in any language. For example, a two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array.
Two-dimensional array Definition
VTwo-dimensional array definition:
§ Type arrayname [] []; or type [] [] arrayname;
VYou can also write this statement:
§ Type [] arrayname [];
VLike a one-dimensional array, no memory space is allocated to the array elements. Similarly, you must use the new operator to allocate memory before accessing each element.
2d array initialization (static)
VTwo-dimensional array Initialization is also divided into static and dynamic.
VStatic initialization: Allocate space to the array while defining the array.
Int intarray [] [] ={{ 1, 2}, {2, 3}, {3, 4}; // you do not need to specify the size of each dimension of the array, the system automatically calculates the size of each dimension of the array based on the number of initial values given during initialization.
Two-dimensional array initialization (dynamic)
VDynamic initialization: For multi-dimensional arrays, there are two ways to allocate memory space:
§ Directly allocate space for each dimension;
VFrom the maximum dimension (and must start from the maximum dimension), allocate space for each dimension, for example:
Int ary [] [] = new int [2] [];
Ary [0] = new int [2];
Ary [1] = new int [3];
Ary [0] [0] = 11; Ary [0] [1] = 12;
...
VWe can see from the above that the high-dimensional array is a special one-dimensional array;
VObtain the length of the first dimension of a high-dimensional array: ary. length; obtain the length of the first element array in the first dimension: ary [0]. length; and so on;
Supplement: Java array initialization details:
One-dimensional array
1) int [] A; // declaration, not initialized
2) int [] A = new int [5]; // The default value is initialized, And the int type is 0.
3) int [] A = {1, 2, 3, 4, 5}; // initialize to a given value
4) int [] A = new int [] {1, 2, 3, 4, 5}; // same as (3)
Int [] A = new int [5] {1, 2, 3, 4, 5}; // error. If array Initialization is provided, the dimension expression cannot be defined.
5) int [];
A = new int [5]; // correct, same as (2)
Int [];
A = {1, 2, 3, 4, 5}; // error. The array constant can only be used in initialization, for example, (3)
6) int A [];
A [0] = 1; // error. The value cannot be assigned because the array is not initialized.
A [1] = 2;
Two-dimensional array
1) int [] [] A; // declaration, not initialized
2) int [] [] A = new int [2] [3]; // The default value is initialized, And the int type is 0.
3) int [] [] A = {1, 2}, {2, 3}, {3, 4}; // initialize to a specified value
Int [] [] A = {1, 2}, {2, 3}, {3, 4, 5}; // no error. The array space is not allocated consecutively, therefore, the size of each dimension is not required to be the same.
4) int [] [] A = new int [2] [];
A [0] = new int [3]; // A [0] is actually an array
A [1] = new int [4]; // the size of each dimension can be different;
5) int [] [] A = new int [] [] {1, 2}, {2, 3}, {3, 4, 5}; // same as (3)
Int [] A = new int [5] {1, 2}, {2, 3}, {3, 4, 5}; // error. If array Initialization is provided, the dimension expression cannot be defined.
Int [] [] A = new int [2] [];
A [0] = {1, 2, 3, 4, 5}; // error. The array constant can only be used in initialization.
6) int [] [] A = new int [2] [];
A [0] [1] = 1; // error. The second-dimension is not initialized and cannot be assigned a value. java. Lang. nullpointerexception is abnormal.
Conclusion: 1. Two-Dimensional is the array, and the size of the array is not the same.
2. No matter whether it is one-dimensional or two-dimensional, Initialization is required before use (assignment, access). You can use new to initialize by default or use array constants.
1. Dynamic initialization: the array definition is separated from the operations for allocating space and assigning values to arrays;
2. Static initialization: when defining numbers, allocate space for array elements and assign values;
3. Default initialization: The array is a reference type, and its elements are equivalent to member variables of the class. Therefore, after the array is allocated space, each element is also initialized by the hermit according to the rules of the member variables.
Instance:
Testd. Java (dynamic)
Program code
Public class testd
{
Public static void main (string ARGs []) {
Int A [];
A = new int [3];
A [0] = 0;
A [1] = 1;
A [2] = 2;
Date days [];
Days = new date [3];
Days [0] = new date (2007,4, 5 );
Days [1] = new date (2007,2, 31 );
Days [2] = new date (2007,4, 4 );
}
}
Class date
{
Int year, month, day;
Date (INT year, int month, int day ){
This. Year = year;
This. month = month;
This. Day = Day;
}
}
Tests. Java (static ):
Program code
Public Class Tests
{
Public static void main (string ARGs []) {
Int A [] = {0, 1, 2 };
Time Times [] = {New Time (, 42), new time (, 54), new time (, 2 )};
}
}
Class time
{
Int hour, Min, SEC;
Time (INT hour, int min, int Sec ){
This. Hour = hour;
This. min = min;
This. sec = sec;
}
}
Testdefault. Java (default ):
Program code
Public class testdefault
{
Public static void main (string ARGs []) {
Int A [] = new int [5];
System. Out. println ("" + A [3]);
}
}