Skip to content
Advertisement

2D Array where each element is a List

I’m trying to write a program where I have a 2D array, stored_variables[][], where each element of stored_variables is a list rather than a normal element. I know how to make a 2D array of lists, but not how to do this.

Advertisement

Answer

You just create your 2D array, and put another array inside each field. So basically you end up with a 3D array:

int[][][] arr = {
  {
    {1, 2, 3}, 
    {4, 5, 6}
  }, 
  { 
    {7}, 
    {8, 9}, 
    {10}
  } 
};

Now you have an Array, that contains 2 Arrays. And the first of those 2 Arrays contains another 2 Arrays (with 3 values each). And the second one contains 3 Arrays (with 1, 2 and 1 values).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement