0

What I am trying to do is creating a list (array dimensions) with multiple groups (array rows), based on user input. For example:

// get list number;
Scanner input = new Scanner(System.in);
System.out.println("How many lists do you want to create?");
int listNum = input.nextInt();

// listNum = 4
// create array with 4 dimensions;

// get  list size;
for (int i=0; i<listNum; i++) {
    System.out.println("How many entries are in list No."+(i+1)+" ?");
    // TBC
}
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
DubbleV
  • 23
  • 4
  • 1
    Are you sure an array with x dimension is a suitable data collection for your need here? It sounds rather impractical to create an array with that many dimensions. – Tom Mar 04 '21 at 16:56
  • Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – esqew Mar 04 '21 at 16:57
  • 1
    Do you mean create a custom 2d array? or custom any dimension array? – bdehmer Mar 04 '21 at 16:59
  • 1
    I agree with @bdehmer. Based on the structure and questions in the code, I think they simply want a 2D array where each row has possibly a different number of entries; otherwise known as a [jagged array](https://www.geeksforgeeks.org/jagged-array-in-java/). – Idle_Mind Mar 04 '21 at 18:07
  • I know you're learning, but lists are different than arrays here, and would not require the pre-filling that you're doing. – Rogue Mar 04 '21 at 18:21

3 Answers3

2

Here's an example of creating a jagged array of ints:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("How many lists do you want to create? ");
    int listNum = input.nextInt();

    int[][] data = new int[listNum][];

    // get number of entries for each row
    for (int i=0; i<data.length; i++) {
        System.out.print("How many entries are in list No. "+(i+1)+" ? ");
        int numEntries = input.nextInt();
        data[i] = new int[numEntries];
    }

    // demonstrate that it worked:
    for (int i=0; i<data.length; i++) {
        for(int j=0; j<data[i].length; j++) {
            System.out.print(data[i][j] + " ");
        }
        System.out.println();
    }
} 

Sample output:

How many lists do you want to create? 3
How many entries are in list No. 1 ? 5
How many entries are in list No. 2 ? 7
How many entries are in list No. 3 ? 2
0 0 0 0 0 
0 0 0 0 0 0 0 
0 0
Idle_Mind
  • 33,113
  • 3
  • 25
  • 33
1

Well, you could do something like this, but it's nasty as hell. Assuming 4 is max.

int dim = input.nextInt(); //4
Object wtfarray = getArray(dim,2);
putAvalueOnFirstJustForFun(wtfarray,dim,-99);
//throw new Exception("Please kill me");

/*wtfarray 
    
    [[[[-99, 0], [0, 0]], [[0, 0], [0, 0]]], 
     [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]]
*/


//...
void putAvalueOnFirstJustForFun(Object array, int dim, int value)
{
  switch(dim)
  {
    case 1:  {((int[])array)[0] = value; return;}
    case 2:  {((int[][])array)[0][0] = value; return;}
    case 3:  {((int[][][])array)[0][0][0] = value; return;}
    //...until your max    
    default: {((int[][][][])array)[0][0][0][0] = value;}
}

static Object getArray(int dimensions, int size)
{
  switch (dimensions)
  {
     case 1:  {return new int[size];}
     case 2:  {return new int[size][size];}
     case 3:  {return new int[size][size][size];}
     //...until your max    
     default: {return new int[size][size][size][size];}
   }
}

This thing is ugly, please do not do it.

aran
  • 9,202
  • 4
  • 30
  • 62
  • In reality, you could just abstract the actual math behind a contiguous array. If you want to add a dimension, it'll be another subdivision of your available indicies. (so, modulo and multiplying maths). Abstracting that to a class would then rid you of the infinite switch. Neat solution for something simple though (<10D) – Rogue Mar 04 '21 at 18:22
  • @Rogue I think I've created one of the ugliest codes ever, you're being so benevolent with the term *Neat* `; )` – aran Mar 04 '21 at 18:51
1

Assuming you are mixing up dimensions with rows... then you're on to it. Just capture the columns (for you listNum) then rows with another int variable. Then create the array like you normally would.

int[][] customArray = new int[listNum][rowNum];
bdehmer
  • 503
  • 1
  • 3
  • 15