user11518995
user11518995

Reputation:

How to create an array that holds arrays in java

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("\n");
    char[][] array = new char[1][];

    for (int x = 0; x < 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)

Upvotes: 0

Views: 351

Answers (2)

sbsatter
sbsatter

Reputation: 581

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

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

    for (int x = 0; x < 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[x].toCharArray();

You can find a modified version below:

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; // TO DO
}

Upvotes: 0

Harry Coder
Harry Coder

Reputation: 2730

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("\n");
List<String> array = new ArrayList<>();

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

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

Upvotes: 0

Related Questions