Dimitra Micha
Dimitra Micha

Reputation: 2129

Java: Storing values in 2D Array

I have a problem with storing values in a multidimensional array. The main concept of the idea is that I have an arraylist which is called user_decide and I transform it in a array. So, the decide array looks like [1, 45, 656, 8, 97, 897], but all the rows don't have the same number of elements. Then, I split this replace [,] and spaces and I would like to store each value individually in a 2D array. So, I split it with the "," and try to store each value in a different position. Everything seems to be printed great, even the cut[j] is what I want to store, but I get a java.lang.NullPointerException, which I don't get. The count variable is actually the count = user_decide.size()

String [] decide = user_decide.toArray(new String[user_decide.size()]);
for (int i = 0; i < count; i ++){
   decide[i] =
      decide[i].replaceAll("\\s", "").replaceAll("\\[","").replaceAll("\\]", "");
}
String [][] data1 = new String[count][];
for (int i = 0; i < count; i++){
   String [] cut = decide[i].split("\\,");
   for (int j = 0; j < cut.length; j++){
      System.out.println(cut[j]);
      data1[i][j] = cut[j];
   }
}

Another question is why I cannot store it in a Int [][] array? Is there a way to do that?

Thank you a lot.

** EDIT **

I just made an edit about my answer after I accepted the question. I am trying to store it in a 2D int array.

String [][] data1 = new String[user_decide.size()][];
int [][] data = new int [user_decide.size()][];

for (int i = 0; i < user_decide.size(); i++){
    data1[i] = decide[i].split("\\,");
    for (int j = 0; j < data1[i].length; j++) {
        data[i] = new int [data1[i].length]; 
        data[i][j] = Integer.parseInt(data1[i][j]);
        System.out.println(data1[i][j]);
    }
}

Upvotes: 0

Views: 12215

Answers (2)

jlordo
jlordo

Reputation: 37843

Ivaylo Strandjev's answer shows the reason for your problem. But there's a much simpler solution:

String [][] data1 = new String[count][];
for (int i = 0; i < count; i++){
  data1[i] = decide[i].split("\\,");
  System.out.println(Arrays.toString(data1[i]));
}

Also, you don't need to escape the comma.

EDIT

Saw your edit. There is a big mistake, see my comment in your code:

String [][] data1 = new String[user_decide.size()][];
int [][] data = new int [user_decide.size()][];

for (int i = 0; i < user_decide.size(); i++){
    data1[i] = decide[i].split("\\,");
    for (int j = 0; j < data1[i].length; j++) {
        data[i] = new int [data1[i].length]; // This line has to be prior to the
        // inner loop, or else you'll overwrite everything but the last number.
        data[i][j] = Integer.parseInt(data1[i][j]);
        System.out.println(data1[i][j]);
    }
}

If all you want is the int[], this is what I would do:

int [][] data = new int [user_decide.size()][];

for (int i = 0; i < user_decide.size(); i++){
    String[] temp = decide[i].split(",");
    data[i] = new int [temp.length];
    for (int j = 0; j < temp.length; j++){
        data[i][j] = Integer.parseInt(temp[j]);
        System.out.println(data1[i][j]);
    }
}

There are probably nicer ways, but I don't know why you are using user_decide.size() ( a Collection) for the condition and decide[i] (an array) within the loop. There's no good reason I can think of mixing this, as it could lead to errors.

Upvotes: 3

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 71009

In java you will need to also allocate data[i], before copying contents:

for (int i = 0; i < count; i++){
  data1[i] = new String[cut.length];
  String [] cut = decide[i].split("\\,");
    for (int j = 0; j < cut.length; j++){
      System.out.println(cut[j]);
      data1[i][j] = cut[j];
    }
}

Before copying contents:

Upvotes: 2

Related Questions