如何在Java中创建一个包含数组的数组。

huangapple go评论87阅读模式
英文:

How to create an array that holds arrays in java

问题

以下是翻译好的内容:

我正在尝试创建一个包含多个字符数组的字符数组。以下是该方法,当我运行它时,会出现一个错误,错误消息为:“char[] 无法转换为 char”:

private MazeStatus[][] stringToMaze(String sMaze) {

    String[] splitString = sMaze.split("\n");
    char[][] array = new char[1][];
    
    for (int x = 0; x < splitString.length; x++) {
        array[1][x] = splitString[x].toCharArray();
    }
    return null; // TO DO
}

我该如何使其保存字符数组,而不仅仅是字符?

我在 Stack Overflow 上搜索了类似的问题,并找到了一个与我问题标题相同的帖子。其中一个答案是这样的:

String[][] arrays = { array1, array2, array3, array4, array5 };

我理解上述的方法,然而我不能这样做,因为我首先必须遍历字符串数组,将数组中的每个字符串转换为字符数组,然后才能将其添加到保存数组的数组中(但我不确定如何在不初始化具有数组的数组的情况下执行此操作,正如上面的答案所述)。

英文:

I am attempting to make an char array which holds multiple char arrays. Below is the method, when I run it I get a error that says "char[] can not be converted to char":

private MazeStatus[][] stringToMaze(String sMaze) {

    String[] splitString = sMaze.split(&quot;\n&quot;);
    char[][] array = new char[1][];
    
    for (int x = 0; x &lt; splitString.length; x++) {
        array[1][x] = splitString[x].toCharArray();
    }
	return null; // TO DO
}

How do I make it so that it holds char arrays apposed to just chars?

I have searched stack overflow for similar question and found one with the same title as mine. One of the answers were this:

String[][] arrays = { array1, array2, array3, array4, array5 };

I understand the above method, however I am not able to do that as I first have to loop through the string array, convert each String in the array to a char array and then can I only add it to the array that holds arrays (which I am not sure how to do without initializing it with the arrays as the above answers says)

答案1

得分: 1

你对Java有许多误解。我将通过行号进行演示。

private MazeStatus[][] stringToMaze(String sMaze) {
    String[] splitString = sMaze.split("\n"); // 第1行
    char[][] array = new char[1][]; // 第2行

    for (int x = 0; x < splitString.length; x++) {  // 第3行
        array[1][x] = splitString[x].toCharArray(); // 第4行
    }
    return null; // 待完成
}

我假设您想要输入并将其转换为MazeStatus对象(在您的问题中似乎缺失)。在代码的以下部分:

1 - 您获取迷宫的每一行,每行由字符表示。

2 - 您声明了一个二维字符数组,但只初始化了一个维度。如果您注意到,您会发现数组的另一个维度仍然未知。

3 - 您遍历了迷宫的每一行。

4 - 您将行(splitString[x])转换为字符数组。您采用了二维数组的第一个维度(尽管您的代码将该维度初始化为长度为1)。如果您仍然没有找到问题,您应该知道Java数组是从0开始索引的。在此处了解更多信息。忽略此错误,您进一步尝试将整个字符数组存储在第二维上,而您尚未初始化此维度。

您需要做的是:

  1. 您需要将第一个维度的长度设置为迷宫中的行数。

  2. 对于每一行(字符串),将第二个数组维度初始化为该字符串的长度,然后设置字符数组[row] = splitString .toCharArray();

您可以在下面找到修改后的版本:

private String stringToMaze(String sMaze) {

    String[] splitString = sMaze.split("\n");
    char[][] array = new char[splitString.length][];
    for (int x = 0; x < splitString.length; x++) {
        array[x] = new char [splitString[x].length()];
        array[x] = splitString[x].toCharArray();
        
        for (int i = 0; i < splitString[x].length(); i++) {
            System.out.print(array[x][i]);
        }
        System.out.println();
    }
    return null; // 待完成
}
英文:

You have many misconceptions on Java. Let me demonstrate with line numbers.

private MazeStatus[][] stringToMaze(String sMaze) {
    String[] splitString = sMaze.split(&quot;\n&quot;); // line 1
    char[][] array = new char[1][]; // line 2

    for (int x = 0; x &lt; splitString.length; x++) {  // line 3
        array[1][x] = splitString[x].toCharArray(); // line 4
    }
    return null; // TO DO
}

I am assuming you want to take an input and convert that to an object of MazeStatus (which appears to be missing with your question).
In Line:

1 - you get each row of the maze, which is represented by characters.

2 - You declare a 2D char array, but only initialize one of the dimensions. If you notice, you will find that the other dimension of the array is still unknown.

3 - You go by each of the rows of the maze.

4 - You convert the row (splitString[x]) to a character array. You take the 1st dimension of the 2D array (although your code initialized this dimension to have length 1). If you still haven't found a problem, you should know that Java arrays are 0-based indexed. Read up more on it. Ignoring this mistake, you further try to store a whole character array on the 2nd dimension, whereas you have not initialized this dimension yet.

So what you need to do:

  1. You need to set the length of the first dimension to the number of rows you will have in the maze.

  2. For each row (of string), initialize the 2nd array dimension to the length of that string, and then set the character array[row] = splitString .toCharArray();

You can find a modified version below:

private String stringToMaze(String sMaze) {

    String[] splitString = sMaze.split(&quot;\n&quot;);
    char[][] array = new char[splitString.length][];
    for (int x = 0; x &lt; splitString.length; x++) {
        array[x] = new char [splitString[x].length()];
        array[x] = splitString[x].toCharArray();
        
        for (int i = 0; i&lt; splitString[x].length(); i++) {=
            System.out.print(array[x][i]);
        }
        System.out.println();
    }
    return null; // TO DO
}

答案2

得分: 0

这个错误是正常的。您正在尝试将一个 Array Object 赋值给一个 char 原始值 array[1][x]。它们是两种不同的类型。

另外 array[1][x] 会抛出一个异常,因为您的数组长度为 1,所以索引应该是 0

我建议您改用 ArrayList,因为您不知道 sMaze 的大小,如果其中一个值是 String,您的 array 中的 char 元素可能无法处理它。

String[] splitString = sMaze.split("\n");
List<String> array = new ArrayList<>();

for (int x = 0; x < splitString.length; x++) {
    array.add(splitString[x]);
}

但根据您想要做的事情,您也可以使用 ArrayListChar 类型。

英文:

This error is normal. You are trying to affect an Array Object to a char primitive value array[1][x]. They are two different types.

Also array[1][x] will throw an exception because the length of your array is 1 so the index should be 0.

I would suggest you to use ArrayList instead, because your don't know the size of sMaze and if one of the value is a String, your char element in array could not handle it.

String[] splitString = sMaze.split(&quot;\n&quot;);
List&lt;String&gt; array = new ArrayList&lt;&gt;();

for (int x = 0; x &lt; splitString.length; x++) {
    array.add(splitString[x]);
}

But depending on what you want to do, you can use and ArrayList of Char.

huangapple
  • 本文由 发表于 2020年4月4日 15:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61024900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定