The basic use of format array 0912

The basic use of format array 0912

Array definition

Case 1, declare first, then allocate space

类型[] 数组名;
数组名 = new 类型[长度];

Case two, declare and allocate space

类型[] 数组名 = new 类型[长度];

Case three, declaration and assignment (all)

类型 数组名 = new 类型[]{
    
    数据1,数据n}

Case four, declaration and assignment (simplified)

类型[] 数组名 = {
    
    数据1,数据n}

Assignment and change of the corresponding pit position of the array subscript

数组[下标] =

Obtaining the corresponding value of the array subscript

数组[下标]

Array traversal print all members

        for (int i = 0; i < 数组名.length; i++) {
    
    
            数组名[i] 的相关操作;
        }

Array sort

Ascending

import java.util.Arrays;

Arrays.sort(数组名);

Reverse order

        int 数组长度 = 数组名.length;
        int 最大索引 = 数组长度 - 1;
        for (int i = 0; i < 数组长度 / 2; i++) {
    
    
            int temp = 数组名[i];
            数组名[i] = 数组名[最大索引 - i];
            数组名[最大索引 - i] = temp;
        }

Append a data to the string array

        for (int i = 0; i < 数组名.length; i++) {
    
    
            // 判断下标对应值是否为null
            if (数组名[i] == null) {
    
    
                // 进入分支表明,当前下标是空值,可占领
                数组名[i] = 要插入的数据;
            }
        }

Use of object array

// 只声明不赋值
类名称[] 数组名 = new 类名称[长度];

Guess you like

Origin blog.csdn.net/ifubing/article/details/108546520