0

I want to create a 2d array that is made of smaller 2d integer arrays, to overall make a matrix graph. Instead of storing integers in the bigger array I would store 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.

I was thinking something like 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.

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?

So how could I got about this?

Thanks in advance!

2
  • You could do int[][][] array3D; Commented Apr 16, 2020 at 19:12
  • @junvar How would that work? Since my inner array is only a 2d array. Commented Apr 16, 2020 at 19:15

2 Answers 2

1

In Java multi-dimentional arrays are implemented as array of arrays approach and not matrix form. 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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

It seems 4D array, use int[][][][] to store data.

4D array means 2D array of 2D array

Example: 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.

2 Comments

I apologize, my post was misleading/asked incorrectly I edited to explain better what I want to create.
Thanks, that makes sense. I just want to clarify, the indices: int[outer row][outer column][inner row][inner column] where outer is the outer array and inner is the inner array?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.