[Java getting started notes] Java language basics (5): array, Getting Started java
Introduction
Arrays can be used to store and store multiple data. Java arrays require that all array elements have the same data type. Once array Initialization is complete, the space of the array in the memory is fixed, and the length cannot be changed. Even if the elements of the array are cleared, the occupied space is retained.
Life case: a museum shelf is an item of the same type and its length remains unchanged. Even if an item is removed from the shelf, the shelf remains unchanged. Define an array
Use arrays in four steps:
1. Declared array Java supports two formats of array definition formats:
Type [] variable name; type variable name [];
Example:
Int [] a; int B []; // both methods are supported. However, we recommend that you use the first method.
The Declaration array is equivalent to buying a custom object shelf (array) in the above example to the museum, but the size of the object shelf (array size) has not been determined ), there are no items (array elements) on the shelf ).
2. Allocate space to allocate space, that is, initialize the array. There are two initialization methods: the first method: static initialization syntax format:
Data Type [] variable name = new data type [] {data 1, data 2, data 3 ,......};
Example:
Int [] a = new int [] {, 64, 12, 3}; // store these numbers in an array. The size (length) of the array is the number of elements in curly brackets, in this example, the size is 5 String [] B = new String [] {"Zhang San", "Li Si", "Wang Wu "}; // all data types must be defined as int [] c = {2, 5, 7}; // you can also use this shorthand Method
This is like buying a custom shelf, and placing the display products in the shelf for purchase. The size of the shelf is as large as the size of the shelf. Type 2: Dynamic Initialization
Dynamic initialization only specifies the size of the array. The system specifies the initial value for each element. The syntax format is as follows:
Data Type [] variable name = new data type [size];
In this format, you need to specify the size of an array of the integer type. After the value is specified, the length of the data and the number of elements that the array can store are determined. Specify a default initial value for all elements.
Example:
Int [] a = new int [5]; // defines an array with a length of 5. That is, it can store five int data types. At the same time, the default value of 5 is 0.
During initialization, the default value is set as follows:
- Byte, short, int, and long are 0 by default;
- Float and double are 0.0 by default;
- Char is '\ u0000' by default ';
- Boolean is false by default;
- The default value of other reference types is null;
3. Although you have initialized the array, you can change the data in the array by assigning values. So the array has been initialized and the size of the array has been determined. How can we assign values to no array element? There is also a sub-Object Concept in the array. During array initialization, each element is assigned a subscript, which is the same as the number on the item display shelf. We can re-assign values to each element by subscript. The subscript of the array starts from 0, that is, the subscript of the first element is 0. As to why it starts from 0, we will discuss it later. Let's first look at the example of the Value assignment:
Int [] a = new int [3]; // defines an int type array with a length of 3. The default values of the three elements in the array are 0. a [0] = 4; // set the first element value to 4a [1] = 5; // set the second element value to 5a [2] = 7; // set the third element value to 7
Why does the array subscript start from 0?
First, we need to know that the program runs in the computer memory. When our program starts to process data, it will open up a small space in the memory to store data, the Code also defines a variable, such:
int a = 5;
Defines an int type variable with a value of five. In the memory, it indicates that a small memory is allocated in the memory, and the name is a. The value stored in the memory is 5.
What if we get the value in memory through a variable name such as? It is because each small block of memory has an address, just like an address in our home. Through this address, we can know who lives in it.
The second array is stored in the memory in a connected location to facilitate operations on the array. Let's look at an example:
Int [] B = new int [3]; // defines an array of the int type. The length is 3 and the default value of the element is 0.
In this case, the memory is like this:
The same is true for retrieving data from arrays. How can we know the data of each element in array B? First, we know where the array B is in the memory. Because the array is a memory space connected to each other in the memory, the position of B [0] is equal to B + 0, the position of B [1] is B + 1, B [2] = B + 2 ,...... and so on, knowing the positions of all elements in the array. The first element in the array is exactly the position where the header is opened in the array. It is represented by B [0], which is exactly the position represented by B, and 1 is used to represent the position of the first element, it must be expressed as a + 1-1.
4. Process Data
In the previous content, we have defined an array, initialized an array, and assigned a value. How can we use data? In fact, we have analyzed it before and assigned a value to an array through an array subscript, you can also obtain the value of each array element through the array subscript.
Int [] a = new int [3]; a [0] = 3; a [1] = 4; a [2] = 5; System. out. println (a [1]); // obtain the value of the second position of the array, output it // We can also traverse each element in the array through loop (int I = 0; I <. length; I ++) {System. out. println (a [I]); // print each element in the array in a loop}
In the preceding example, we can see that. length, through the array variable name. length can get the length of the array, so we use. the length of the array is 3, and the elements of the array are printed three times in a loop.
No nonexistent subscript should be accessed
As we know above, the array is controlled by the variable name [subscript] to assign values to the variable or get the value. If the access to the lower mark exceeds the length of the array, an exception will occur.
Int [] a = new int [2]; a [5] = 2; // The length of the array is only 2, And we access 6th elements through subscript 5, if the actual a array does not exist, an exception occurs.
Two-dimensional array or multi-dimensional array
All of the above is about storing a basic data type or reference data type in the array, and we can also store the array in the array:
We have stored another array element in an array element, which stores the int value of a data type. We call this array a two-dimensional array.
Definition, initialization, and assignment of two-dimensional arrays
Data Type [] [] array name = new data type [length 1] [length 2];
Length 1 is the length of an array on the outside, and the length of one layer on the two sides.
The format in definition and initialization can be as follows:
Int [] [] a = new int [3] [3]; a [0] [0] = 2; // The number in the square brackets before the access indicates the element subscript of an array, the number in the second square brackets indicates the subscript a [0] [1] = 3; a [0] [2] = 4; a [1] [0] = 5; a [1] [1] = 3; a [1] [2] = 9; a [2] [0] = 90; a [2] [1] = 70; a [2] [2] = 85;
We can also define a two-dimensional array as a format:
Int [] [] a = new int [3] []; // when defining initialization, set the length of an external layer to 3 without setting the length of one layer,
A [0] = new int [2]; // initialize each layer and set the length to a [1] = new int [1]. a [2] = new int [3]; a [0] [0] = 3; // assign a value to each element a [0] [1] = 4; a [1] [0] = 9; a [2] [0] = 90; a [2] [1] = 70; a [2] [2] = 85;
3D arrays store arrays in arrays ~~~~~ Likewise, there can be n-dimensional arrays, but most people use less ~~~ ^_^ ~~~