Home  >  Article  >  Java  >  Detailed introduction and example analysis of Java two-dimensional arrays

Detailed introduction and example analysis of Java two-dimensional arrays

WBOY
WBOYforward
2023-04-26 09:01:07900browse

What is an array

An array (Array) is an ordered sequence of elements. If a limited collection of variables of the same type is named, then the name is the array name. The individual variables that make up an array are called components of the array, also called elements of the array, sometimes also called subscript variables/12713827). The numeric number used to distinguish the individual elements of an array is called a subscript. In programming, an array is a form of organizing several elements of the same type in an orderly manner for the convenience of processing. These ordered collections of similar data elements are called arrays. Arrays are collections used to store multiple data of the same type.

Example (equipment column)

Detailed introduction and example analysis of Java two-dimensional arrays

Array, elements and subscripts:

For example, when playing King of Glory, everyone must produce equipment , each one has its own equipment slot. Then this equipment column is an array, the equipment in it is the element, and the position where the equipment is placed is the subscript. That is to say, each subscript corresponds to an equipment, and the subscript starts from 0, so the subscript corresponding to the first equipment is 0

Declaration array

int type

Fix the array length when declaring the array, and the length of the array remains unchanged. There are two declaration methods, the first is direct assignment when declaring. The second type does not assign a value when declared, but has a fixed length. Although there is no assignment, all elements will be assigned a value of 0 by default.

public class Test {
    public static void main(String[] args) {
        //声明int类型数组并初始化赋值
        int[] a={1,2,3,4,5,};
        //声明数组设值数组长度,并初始化全为0
        int[] b=new int[10];
    }
}

String type

There is no difference from the above and there are two declaration methods.

public class Test {
    public static void main(String[] args) {
        //声明int类型数组并初始化赋值
        int[] a={1,2,3,4,5,};
        //声明数组设值数组长度,并初始化全为0
        int[] b=new int[10];
        //声明String类型数组并初始化赋值
        String[] d={"aa","bb","cc"};
        //声明数组固定长度,默认初始化全为0
        String[] c=new String[10];
    }
}

Array operation

Traverse the array

Traverse the array: two methods, for loop and for in loop

for loop, here I am in the array Three elements are placed, namely equipment. Loop output, starting from the subscript 0. zb.length is the size of this array

public class Test {
    public static void main(String[] args) {
       String[] zb={"冷静之靴","泣血之刃","名刀司命"};
        for (int i = 0; i < zb.length; i++) {
            System.out.println(zb[i]);
        }
    }
}

Result:

Detailed introduction and example analysis of Java two-dimensional arrays

for in loop, forgot For how to use this cycle, you can refer to the previous article. The Golden Elixir article has a detailed introduction.

public class Test {
    public static void main(String[] args) {
       String[] zb={"冷静之靴","泣血之刃","名刀司命"};
        for (String s : zb) {
            System.out.println(s);
        }
    }
}

Two-dimensional array

A two-dimensional array is essentially an array with an array as an array element, that is, an "array of arrays", type specifier array name [constant expression] [constant expression Mode]. A two-dimensional array is also called a matrix, and a matrix with equal numbers of rows and rows is called a square matrix. Symmetric matrix a[i][j] = a[j][i], diagonal matrix: There are zero elements outside the main diagonal of an n-order square matrix.

A two-dimensional array is an ordinary one-dimensional array. Each element is a one-dimensional array, and the combination is a two-dimensional array.

Detailed introduction and example analysis of Java two-dimensional arrays

Continue using the previous example. At the beginning of each game, one side's data panel has a default sorting (the panel showing equipment and economy). Each person has an equipment slot, which is equivalent to an array. Then there are five equipment columns (one team) on the information panel, and they are arranged in the default order, which is also equivalent to an array. An equipment slot counts as one element, and the position of the equipment slot is the subscript. But each element in this array is also an array, so the data panel is equivalent to a two-dimensional array.

Declaring a two-dimensional array

There is no difference between declaring a two-dimensional array and declaring an array. There are two situations.

public class Test {
    public static void main(String[] args) {
        //声明二维数组并赋值
        int[][] a={{123},{456},{789}};
        //声明二维数组固定大小
        int[][] ns = new int[3][5];
    }
}

Some readers may still be confused by using the King of Glory to introduce the two-dimensional array. I'm a little confused, now type out the above example in code.

I can’t remember the name of the equipment here. I copied the equipment of the next three people directly

public class Test {
    public static void main(String[] args) {
          //五个人,每个人装备栏有三个装备。
        String[][] wzry=new String[5][3];
        //给第一个人买装备,就是给第一个数组赋值
        wzry[0][0]="宝石";
        wzry[0][1]="血刀";
        wzry[0][2]="金身";
        //给第二个人买装备,就是给第二个数组赋值
        wzry[1][0]="铁剑";
        wzry[1][1]="草鞋";
        wzry[1][2]="护甲";
        //给第三个人买装备,就是给第三个数组赋值
        wzry[2][0]="宝石";
        wzry[2][1]="血刀";
        wzry[2][2]="金身";
        //给第四个人买装备,就是给第四个数组赋值
        wzry[3][0]="宝石";
        wzry[3][1]="血刀";
        wzry[3][2]="金身";
        //给第五个人买装备,就是给第五个数组赋值
        wzry[4][0]="宝石";
        wzry[4][1]="血刀";
        wzry[4][2]="金身";
    }
}

Now let’s run it and see what everyone’s equipment has

//第三个人的第二个装备
        System.out.println("第三个人的第二个装备");
        System.out.println(wzry[2][1]);
        //第一个人的第三个装备
        System.out.println("第一个人的第三个装备");
        System.out.println(wzry[0][2]);
        //第五个人的全部装备
        System.out.println("第五个人的全部装备");
        for (int i = 0; i < 3; i++) {
            System.out.println(wzry[4][i]);
        }

result:

Detailed introduction and example analysis of Java two-dimensional arrays

The above is the detailed content of Detailed introduction and example analysis of Java two-dimensional arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact [email protected] delete