Java array (simple and easy to understand)

One, the overview of the array

1. Definition

Array (Array) is a collection of multiple data of the same type arranged in a certain order, and named with a name, and these data are managed uniformly by numbering

2. Array related concepts

  • Array name

  • element

  • index

  • Length: the number of elements

3. The characteristics of the array

The array is ordered

The array itself is a reference data type, but the elements of the array can be either a basic data type or a reference data type

Creating an array object will open up a whole continuous space in memory, and the array name refers to the first address of this continuous space

Once the length of the array is determined, it cannot be changed

4. Classification of arrays

Generally divided into one-dimensional array and two-dimensional array. . . . . .

Second, the use of one-dimensional arrays

1. Declaration and initialization of one-dimensional array

//静态初始化:数组的声明和数组元素赋值操作同时进行
int[] ids = new int[]{
    
    1001,1002,1003};
//动态初始化:数组的声明和数组元素赋值操作分开进行
String[] names = new String[3];

2. How to call the element at the specified position of the array

Array calls the specified position element by index , and can be assigned or viewed.

The index of the array starts from 0 and ends at the length of the array-1

When using indexes, we often encounter array out-of-bounds exceptions. The error message is java.lang.ArrayIndexOutOfBoundsException

String[] names = new String[3];
names[0] = "jak";
names[1] = "ros";
names[2] = "jack";

name[3] = "tom";	//java.lang.ArrayIndexOutOfBoundsException

3. How to get the length of the array

Pass the attributes of the array length

String name = new String[5];

name.length() Output 5

4. How to traverse the array

  • for loop traversal

    int[] ids = new int[10];
    
    for(int i = 0;i < ids.length;i++){
          
          
        System.out.println(ids[i]);
    }
    
  • Enhanced for loop traversal

    int[] ids = new int[10];
    
    for(int id : ids){
          
          
        System.out.println(id);
    }
    

5. The default initialization value of the array elements

Default initialization means that there is no explicit assignment to the array, but the JVM will assign an initial value to each element of the array by default

  • Array element is integer 0
  • Array elements are floating point type 0.0
  • Array elements are ASCII values ​​of char type 0
  • Array element is Boolean type false
  • Array elements are reference data type null

Three, the use of two-dimensional array

In fact, there is no two-dimensional array in the world, but the elements of a one-dimensional array are a one-dimensional array. We are used to calling the one-dimensional array a two-dimensional array

1. Declaration and initialization of two-dimensional array

//静态初始化
int[][] arr1 = new int[][]{
    
    {
    
    1,2,3},{
    
    2,3},{
    
    3,4,5}};
//动态初始化1
int[][] arr2 = new int[3][2];
//动态初始化2
int[][] arr3 = new int[3][];

2. How to call the element at the specified position of the array

int[][] arr = new int[3][];

arr[1][1];	//java.lang.NullPointerException 报错,空指针异常

3. Get the length of the array

int[][] arr1 = new int[][]{
    
    {
    
    1,2,3},{
    
    2,3},{
    
    3,4,5}};

System.out.println(arr1.length);	//3
System.out.println(arr1[2].length);	//3
System.out.println(arr1[1].length);	//2

4. How to traverse the array

for(int i = 0;i < arr.length;i++){
    
    
    for(int j = 0;j < arr[i].length;j++){
    
    
        System.out.print(arr[i][j])
    }
    System.out.println();	//换行
}

5. The default initialization value of the array elements

First we define a few concepts

int[][] arr = new int[3][4];

//外层元素
arr[1];
arr[2];
//内层元素
arr[1][1];
arr[1][2];

int[][] arr = new int[2][3];

​ The default initialization value of the outer element: address value

​ The default initialization value of the inner element: the same as the initialization of a one-dimensional array

int[][] arr = new int[2][];

​ The default initialization value of the outer element: null

​ The default initialization value of the inner element: cannot be called, otherwise the null pointer is abnormal

Guess you like

Origin blog.csdn.net/weixin_45321793/article/details/109435231