简体   繁体   English

有没有办法使用扫描仪来设置数组中的维数?

[英]is there a way to use a Scanner to set the number of dimensions in an array?

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
}

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:样品 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

Well, you could do something like this, but it's nasty as hell.好吧,你可以做这样的事情,但这太糟糕了。 Assuming 4 is max.假设 4 是最大值。

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.这件事很丑,请不要这样做。

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.只需捕获列(为您listNum )然后使用另一个 int 变量捕获行。 Then create the array like you normally would.然后像往常一样创建数组。

int[][] customArray = new int[listNum][rowNum];

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

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