繁体   English   中英

如何使用用户输入在Java中填充2D数组?

[英]How to populate a 2D array in Java using user input?

我正在尝试通过使用用户输入的字符串填充Java中的二维数组。 我已经有了字符串,并且已经弄清楚如何创建数组。 我只是很难弄清楚如何将值放入数组中。 如果您想知道,是的,我必须使用数组。 这是我到目前为止的内容:

    private static int[][] parseTwoDimension(String strInput)
        {
            int rows = 0; //number of rows in the string

            for (int i = 0; i < strInput.length(); i++)
            {
                if (strInput.charAt(i) == '!') //the ! is an indicator of a new row
                {
                    rows++;
                }
            }

            int[][] arr = new int[rows][]; //create an array with an unknown number of columns
            int elementCounter = 0; //keep track of number of elements
            int arrayIndexCounter = 0; //keep track of array index

            for (int i = 0; i < strInput.length(); i++)
            {
                if (strInput.charAt(i) != '!') //while not the end of the row
                {
                    elementCounter++; //increase the element count by one
                }
                else //reached the end of the row
                {
                    arr[arrayIndexCounter] = new int[elementCounter]; //create a new column at the specified row
                    elementCounter = 0; //reset the element counter for the next row
                    arrayIndexCounter++; //increase the array index by one
                }
            }
        /*
        This is where I need the help to populate the array
        */
        char c; //each character in the string
        int stringIndex = -1; //keep track of the index in the string
        int num; //the number to add to the array
        for (int i = 0; i < arr.length; i++)
        {
            for (int j = 0; j < arr[i].length; j++)
            {
                stringIndex++; //increment string index for next element
                c = strInput.charAt(stringIndex); //the character at stringIndex
                if (c == '!') //if it is the end of the row, do nothing
                {

                }
                else //if it is not the end of the row...
                {
                    String s = Character.toString(c); //convert character to String
                    num = Integer.parseInt(s); //convert String to Integer
                    arr[i][j] = num; //add Integer to array
                }
            }
        }
        return arr; //return a two dimensional array the user defined
    }

任何帮助是极大的赞赏 :)

我已经编辑了您的代码并将其发布在下面。 这是解决方法的概述:

  1. 使用string.split(“!”)用定界符'!'分隔您的String。 这将返回一个数组

  2. 然后,使用string.split(“”)将String拆分为单个字符

代码如下:

private static int[][] parseTwoDimension(String strInput)
{
    String[] rows = strInput.split("!");    
    int[][] arr = new int[rows.length()][];

    for(int i = 0; i < rows.length; i++)
    {
        String[] elements = rows[i].split("");
        int[] intElements = new int[elements.length];

        for(int j = 0; j < elements.length; j++)
        {
            intElements[j] = Integer.parseInt(elements[j]);
        }
        arr[i] = intElements;
    }
}

让我知道这个是否奏效。 将String解析为数组时,string.split()函数非常有用

暂无
暂无

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

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