Detailed explanation of Java two-dimensional array: Declaration and initialization of two-dimensional array, and obtaining the value of two-dimensional array

Create a 2D array

type array[][];
type[][] array;
int[][] age;
char[][] sex;

Initialize 2D array

array=new type[][]{值 1,值 2,值 3,…,值 n};
array=new type[][]{new 构造方法(参数列),…};
type[][] array={{第1行第1列的值,第1行第2列的值,…},{第2行第1列的值,第2行第2列的值,…};

Example 1

int[][] temp;
temp=new int[][]
{
    {1,2},{3,4}
};
int[][] temp;
temp=new int [][]
{
    {new int(1),new int(2)},{new int(3),new int(4)}
};
int[][] temp={{1,4}};

Get a single element

array[i-1][j-1];

Example 2

public static void main(String[] args)
{
    double[][] class_score={{10.0,99,99},{100,98,97},100,99.5},{99.5,98.5}};
    System.out.println("第二行第二列元素的值:"+class_score[1][1]);
    System.out.println("第四行第一列元素的值:"+class_score[3][0]);
}
第二行第二列元素的值:98.0
第四行第一列元素的值:99.5

Get all elements

Example 3

public static void main(String[] args)
{
    double[][] class_score={{100,98.5 }};
    for(int i=0;i<class_score.length;i++)
    {    //遍历行
        for(int j=0;j<class_score[i].length;j++)
        {
            System.out.println("class_score["+i+"]["+j+"]="+class_score[i][j]);
        }
    }
}
class_score[0][0]=100.0
class_score[0][1]=99.0
class_score[0][2]=99.0
class_score[1][0]=100.0
class_score[1][1]=98.0
class_score[1][2]=97.0
class_score[2][0]=100.0
class_score[2][1]=100.0
class_score[2][2]=99.5
class_score[3][0]=99.5
class_score[3][1]=99.0
class_score[3][2]=98.5

Example 4

public class Test11
{
    public static void main(String[] args)
    {
        //创建一个二维矩阵
        int[][] matrix=new int[5][5];
        //随机分配值
        for(int i=0;i<matrix.length;i++)
        {
            for(int j=0;j<matrix[i].length;j++)
            {
                matrix[i][j]=(int)(Math.random()*10);
            }
        }
        System.out.println("下面是程序生成的矩阵\n");
        //遍历二维矩阵并输出
        for(int k=0;k<matrix.length;k++)
        {
            for(int g=0;g<matrix[k].length;g++)
            {
                System.out.print(matrix[k][g]+"");
            }
            System.out.println();
        }
    }
}
34565
96033
48741
10583
63985

Gets the entire line of elements

Example 5

public static void main(String[] args)
{
    double[][] class_score={{100,98.5}};
    Scanner scan=new Scanner(system.in);
    System.out.println("当前数组只有"+class_score.length+"行,您想查看第几行的元素?请输入:");
    int number=scan.nextInt();
    for(int j=0;j<class_score[number-1].length;j++)
    {
        System.out.println("第"+number+"行的第["+j+"]个元素的值是:"+class_score[number-1][j]);
    }
}
当前数组只有4行,您想查看第几行的元素?请输入:
3
第3行的第[0]个元素的值是:100.0
第3行的第[1]个元素的值是:100.0
第3行的第[2]个元素的值是:99.5

Gets the entire column element

Example 6

public static void main(String[] args)
{
    double[][] class_score={{100,98.5}};
    Scanner scan=new Scanner(system.in);
    System.out.println("您要获取哪一列的值?请输入:");
    int number=scan.nextInt();
    for(int i=0;i<class_score.length;i++)
    {
        System.out.println("第 "+(i+1)+" 行的第["+number+"]个元素的值是"+class_score[i][number]);
    }
}
您要获取哪一列的值?请输入:
2
第 1 行的第[2]个元素的值是99.0
第 2 行的第[2]个元素的值是97.0
第 3 行的第[2]个元素的值是99.5
第 4 行的第[2]个元素的值是98.5
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>