0

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)

DaleJV
  • 370
  • 1
  • 3
  • 15
  • You can initialize an array while creating it like this, `char[][] array = new char[1]{'1','2'} ; or char[] [] array = new char[1] [2] ;` –  Apr 04 '20 at 07:27
  • and change splitString. toCharArray() [x] –  Apr 04 '20 at 07:31
  • If you want one row per line with a number of char arrays for each word, you'll need a three-dimensional array. However, at this point I'd suggest using an ArrayList because you don't seem to know how many of each item you need at the time the variable is declared. – NomadMaker Apr 04 '20 at 07:36
  • Appreciate all the help from you two! Completely forgot about ArrayList collection, it will fit my needs perfectly so thank you @NomadMaker – DaleJV Apr 04 '20 at 07:50

2 Answers2

1

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
}
sbsatter
  • 591
  • 3
  • 22
  • Thank you for this really well written answer! I am rather knew to two dimensional arrays and I have found myself struggling with them when I used them in the past.. So I am definitely going to be doing a lot more research on dimensional arrays! I am a bit confused with a certain line in the modified version. You say "array[x] = new char [splitString[x].length()];" which I understand but then you write "array[x] = splitString[x].toCharArray();". Would that not just replace what was in array[x] before? – DaleJV Apr 06 '20 at 01:58
  • No, in java, we do something an initialization and assignment. So, in here, say for x = 2, array[2] = new char [splitString[2].length()] tells the array that in it's second index is another array of size splitString[2].length(). Now, array[2] = splitString[2].toCharArray() will simply assign that character array in array[2]. Without the previous step, array[2] will not know the actual size of the character array in array[2]. @ExileVoid – sbsatter Apr 07 '20 at 04:11
  • Also, in these cases, it is essential that we use ArrayList (or List of Lists) for greater flexibility. The modified solution will work but you will always need to track the corresponding splitString index as well. – sbsatter Apr 07 '20 at 04:12
  • I understand that now, once again I really do appreciate the explanation you have given me. In my classes we have never done any method of initialization like as we have not really spent much time on 2d arrays, so it is really cool to learn this way! I do always tend to use ArrayLists and I did use it for this question and got it all working. I just wanted to know how to do it with a simple 2d array to better my understanding of how that works. – DaleJV Apr 07 '20 at 10:57
  • That is great, happy to have helped! Also, hackerrank has easy problems to get you started. – sbsatter Apr 08 '20 at 03:57
0

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.

Harry Coder
  • 2,429
  • 2
  • 28
  • 32