1. Overview of Arrays (reference types)
- An array is a data structure that stores the same type
- Array variable belongs to reference data type
- The elements in the array can be any data type (base type and reference type)
- Once an array is created, its size cannot be changed , but the elements in the array can be changed .
2. Initialization of arrays
Once an array is created successfully, it will complete the following three actions
- Create an Array object
- Allocating storage space to an array in memory
- Initialize default values for elements of an array
3. Creation of an array of basic data types (when no value is assigned)
Array of type int: default value is 0
Array of type Boolean: false by default
4. Creation of an array of reference data types (when no value has been assigned)
The default value is NULL, because the array is also a reference type, and the element is also a reference type, so the array points to the array element, and the array element points to its value
5. Enhanced for Loop
Added an enhanced for loop syntax in JDK5.0
for (type Element:array)
{System.out.println (element);}
int arr[]={1,32,345,2};
for (int element:arr)
{System.out.println (element+ "");}
Pros: Ideal for traversing the contents of an element in an array or collection
Disadvantage: Unable to access the subscript of an array
6. Two-dimensional array (array of arrays)
Dynamic initialization:
int [] [] a = new int[4][5];
int [] [] b = new int[3][];
B[0] = new INT[2];
B[1] = new INT[3];
B[2] = new INT[5];
? Static initialization:
int [] [] array = {{1,2},{2,3},{3,4,5}};
Declaration and use of the java-array