简体   繁体   English

用字符串输入值填充二维数组

[英]Filling a 2D array with String input values

I'm trying to fill a 2D array of String values with my own user inputs with Scanner .我正在尝试使用我自己的用户输入来填充String值的二维数组Scanner

Scanner s = new Scanner(System.in);
String[][] example = new String[20][10];

for (int x = 0; x < example.length; x++) {
    System.out.println("Enter name " + (x + 1) + ": ");
    example[x] = s.next();
}

I have no issues with filling in [][these ones] , but I'm having issues with the [these ones] .我觉得没有问题灌装[][these ones] ,但我对这个有问题[these ones] I need help with why this doesn't work and what I can do instead to fix it.我需要帮助解释为什么这不起作用以及我可以做些什么来解决它。 I'm not super fluent so please try to keep things on the simpler side.我不是超级流利,所以请尽量保持简单的一面。

Don't forget you are allowed to use for-loops inside of for-loops.不要忘记您可以在 for 循环内使用 for 循环。 You can use this to your advantage to fill 2D-arrays.您可以利用它来填充二维数组。 For example by using this code:例如,通过使用此代码:

Scanner s = new Scanner(System.in);
String[][] example = new String[3][3];

for (int i = 0; i < example.length; i++) {
   for (int j = 0; j < example[0].length; j++) {
        example[i][j] = s.nextLine();
    }
}

When you're filling an array using loops you need two (nested) loops - one for the rows and one for the columns (typically in this order).当您使用循环填充数组时,您需要两个(嵌套)循环 - 一个用于行,一个用于列(通常按此顺序)。

eg:例如:

for (int row=0; row < twoDArray.length; row++) {
    for (int col=0; col < twoDArray[row].length; col++) {
        System.out.print("Enter name [" + (row+1) + "][" + (col+1) + "]: ");
        twoDArray[row][col] = s.next();
    }
}

Row/col are analogous to xy coordinates when drawing a chart. Row/col 类似于绘制图表时的 xy 坐标。

To extend this further you could do the same for 3D, (and 4D, ... [N]D) arrays by adding a 3rd nested array... the looping would begin to get confusing but its exactly the same principal.为了进一步扩展,您可以通过添加第三个嵌套数组对 3D(和 4D,... [N]D)数组执行相同的操作...循环会开始变得混乱,但它的原理完全相同。

Okay, remember that String[][] is an array of an array of strings.好的,记住 String[][] 是一个字符串数组的数组。 That is, type type of example[0] is String[].也就是说,example[0] 的类型类型是 String[]。 So you have to do a few things.所以你必须做一些事情。 This is the code I wrote:这是我写的代码:

public class Example {
    static int X = 5;
    static int Y = 2;

    public static void main(String[] args) {
        String[][] example = new String[X][Y];

        for (int outerIndex = 0; outerIndex < X; ++outerIndex) {
            // THIS LINE IS CRITICAL.
            example[outerIndex] = new String[Y];
            for (int innerIndex = 0; innerIndex < Y; ++innerIndex) {
                example[outerIndex][innerIndex] =
                    new String("("
                        + Integer.toString(outerIndex)
                        + ","
                        + Integer.toString(innerIndex)
                        + ")"
                    );
            }
        }

        for (int outerIndex = 0; outerIndex < X; ++outerIndex) {
            for (int innerIndex = 0; innerIndex < Y; ++innerIndex) {
                System.out.printf("%s    ", example[outerIndex][innerIndex]);
            }
            System.out.printf("\n");
        }
    }
}

Output:输出:

$ javac Example.java && java Example
(0,0)    (0,1)
(1,0)    (1,1)
(2,0)    (2,1)
(3,0)    (3,1)
(4,0)    (4,1)

I didn't want to spew 200 values, so I used a smaller array size.我不想喷出 200 个值,所以我使用了较小的数组大小。

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

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