📜  在Java中确定二维数组的上界

📅  最后修改于: 2022-05-13 01:54:45.408000             🧑  作者: Mango

在Java中确定二维数组的上界

Java中的多维数组很常见,可以称为数组数组。 Java中二维数组中的数据以二维表格形式存储。二维数组是多维数组的最简单形式。二维数组可以看成是一维数组的数组,方便理解。二维数组声明为两个维度,也称为边界。

数组数组的语法:

data_type[][] array_name = new data_type[x][y];

例如: int[][] arr = new int[20][30]; // 20 X 30 数组数组

数组数组的直接声明:

data_type[][] array_name = {
{valueR1C1, valueR1C2, ....},
{valueR2C1, valueR2C2, ....}
};

例如: int[][] arr = {{1, 2}, {3, 4}};

数组数组中值的分配

Java
// Allocation of values in an Array of Arrays
class GFG {
    public static void main(String[] args)
    {
  
        int[][] arr = new int[20][30];
        arr[0][0] = 100;
  
        System.out.println("arr[0][0] = " + arr[0][0]);
    }
}


Java
// Printing complete Array of Arrays
class GFG {
    public static void main(String[] args)
    {
  
        int[][] arr = { { 1, 2 }, { 3, 4 } };
  
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                System.out.println("arr[" + i + "][" + j
                                   + "] = " + arr[i][j]);
    }
}


Java
// Determine the Dimension of an Array of Arrays
class GFG {
    public static void main(String args[])
    {
        int[][] arr = new int[10][20];
        int row_bound = arr.length;
        int column_bound = arr[0].length;
        System.out.println("Dimension 1: " + row_bound);
        System.out.println("Dimension 2: " + column_bound);
    }
}


Java
// row bound and column bounds
class GFG {
    public static void main(String args[])
    {
        int[][] arr = { { 1, 2 },
                        { 3 },
                        { 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12, 13, 14, 15 } };
        int row_bound = arr.length;
        int column_bound1 = arr[0].length;
        int column_bound2 = arr[1].length;
        int column_bound5 = arr[4].length;
        System.out.println("Dimension 1: " + row_bound);
        System.out.println("Dimension 2: " + column_bound1);
        System.out.println("Dimension 3: " + column_bound2);
        System.out.println("Dimension 4: " + column_bound5);
    }
}


输出
arr[0][0] = 100

打印完整的数组数组

Java

// Printing complete Array of Arrays
class GFG {
    public static void main(String[] args)
    {
  
        int[][] arr = { { 1, 2 }, { 3, 4 } };
  
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                System.out.println("arr[" + i + "][" + j
                                   + "] = " + arr[i][j]);
    }
}
输出
arr[0][0] = 1
arr[0][1] = 2
arr[1][0] = 3
arr[1][1] = 4

Java中的Arrays数组其实就是一维数组的数组。二维数组的每一行都有一个长度字段,用于保存列数。但是,我们可以使用以下方法获得行上限和列维度:

int row_bound = array_name.length;
int column_bound = array_name[0].length;

确定数组数组的维数

Java

// Determine the Dimension of an Array of Arrays
class GFG {
    public static void main(String args[])
    {
        int[][] arr = new int[10][20];
        int row_bound = arr.length;
        int column_bound = arr[0].length;
        System.out.println("Dimension 1: " + row_bound);
        System.out.println("Dimension 2: " + column_bound);
    }
}
输出
Dimension 1: 10
Dimension 2: 20

注意:理解Java并没有真正的二维数组是很重要的。 Java数组本质上是锯齿状的。它有数组的数组。因此,列级别没有一个上限/长度。

例子:

Java

// row bound and column bounds
class GFG {
    public static void main(String args[])
    {
        int[][] arr = { { 1, 2 },
                        { 3 },
                        { 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12, 13, 14, 15 } };
        int row_bound = arr.length;
        int column_bound1 = arr[0].length;
        int column_bound2 = arr[1].length;
        int column_bound5 = arr[4].length;
        System.out.println("Dimension 1: " + row_bound);
        System.out.println("Dimension 2: " + column_bound1);
        System.out.println("Dimension 3: " + column_bound2);
        System.out.println("Dimension 4: " + column_bound5);
    }
}
输出
Dimension 1: 5
Dimension 2: 2
Dimension 3: 1
Dimension 4: 7