简体   繁体   English

如何在 java 中创建由 2d int arrays 组成的 2d 数组?

[英]How do I create a 2d array made of 2d int arrays in java?

I want to create a 2d array that is made of smaller 2d integer arrays, to overall make a matrix graph.我想创建一个由较小的二维 integer arrays 组成的二维数组,以整体制作矩阵图。 Instead of storing integers in the bigger array I would store 2d integer arrays.我不会将整数存储在更大的数组中,而是存储 2d integer arrays。

Edit: I think drew the array I want incorrectly.编辑:我认为绘制了我想要的数组不正确。 What I meant was I want to create a grid (matrix - 2d array) where inside each cell of the grid instead of there being stored a int, boolean, etc. I would like to store a 2d int array in each cell of the grid.我的意思是我想创建一个网格(矩阵 - 2d 数组),在网格的每个单元格内部而不是存储一个 int、boolean 等。我想在网格的每个单元格中存储一个 2d int 数组.

I was thinking something like int[int[][]][int[][]].我在想类似 int[int[][]][int[][]] 的东西。 But realized that wouldn't work since the outer array isn't an integer array, it is just a general array made of integer arrays.但是意识到这不起作用,因为外部阵列不是 integer 阵列,它只是由 integer arrays 制成的通用阵列。

I found codes in other questions here that have a 2d array of objects (ex. room[][]) but I don't think that would be necessary since the array I'm trying to make is made of int[][] arrays, correct?我在这里的其他问题中找到了具有二维对象数组的代码(例如 room[][]),但我认为这没有必要,因为我尝试制作的数组是由 int[][] 组成的arrays,对吗?

So how could I got about this?那么我怎么能得到这个呢?

Thanks in advance!提前致谢!

It seems 4D array, use int[][][][] to store data.看起来是 4D 数组,使用int[][][][]来存储数据。

4D array means 2D array of 2D array 4D array 表示 2D 阵列的 2D 阵列

Example: int[][][][] arr = new int[10][20][10][10]示例: int[][][][] arr = new int[10][20][10][10]

It creates a 2D array of 10X20 size where for each cell there is 2D array of 10X10.它创建一个 10X20 大小的二维数组,其中每个单元格都有一个 10X10 的二维数组。

In Java multi-dimentional arrays are implemented as array of arrays approach and not matrix form.在 Java 中,多维 arrays 被实现为 arrays 方法的数组,而不是矩阵形式。 To implement the data structure provided in the request array has to be implemented as below,要实现请求数组中提供的数据结构,必须按如下方式实现,

Data Structure:数据结构:

{{{0,1}, {{0,1},
  {2,3}}, {2,3}},
 {{0,1}, {{0,1},
  {2,3}}, {2,3}}}

Array Declaration and assignment:数组声明和赋值:

public class MyClass {
    public static void main(String args[]) {

      int[][][][] q =new int[2][2][2][2];

      q[0][0][0][0] = 0;
      q[0][0][0][1] = 1;
      q[0][0][1][0] = 0;
      q[0][0][1][1] = 1;
      q[0][1][0][0] = 2;
      q[0][1][0][1] = 3;
      q[0][1][1][0] = 2;
      q[0][1][1][1] = 3;

      q[1][0][0][0] = 0;
      q[1][0][0][1] = 1;
      q[1][0][1][0] = 0;
      q[1][0][1][1] = 1;
      q[1][1][0][0] = 2;
      q[1][1][0][1] = 3;
      q[1][1][1][0] = 2;
      q[1][1][1][1] = 3;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM