0

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]);
    }
}
3
  • 1
    I can't see where you ever create the second dimension arrays for data1. Commented Feb 13, 2013 at 16:52
  • I'm not entirely sure what you're accomplishing with user_decide.toArray(). If you were passing in the array you wanted to assign it to, that'd be one thing. But why are you passing in an initialized array and returning it? Returning the length would be more straightforward. Commented Feb 13, 2013 at 16:53
  • Okey, you are right, I will just redo the edit. I just fixed it @jlordo . Sorry for the mess. Commented Feb 14, 2013 at 10:28

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @jlordo. Is there a way to save it as Int [][] array? I mean, data1 could it be Int?
Well I will just edit my question and if you think there is a more clean way, I would appreciate if you could tell me:)
@DimitraMicha: See my edit. Also, why are you using different variables for the loop condition and within the loop? user_decide vs decide?
Thank you for your edit. There is no difference in user_decide.size and decide.length, since String [] decide = user_decide.toArray(new String[user_decide.size()]); but I will try to keep on of them only:) I just deleted my edit.
I don't understand your edit. If a problem is solved (and it was), and you have a new question, post a new one. Because now my answer doesn't make sense to anyone else anymore, since it's an answer to a different (your previous) question. Please post a new question with you current code, and all the information about the error you are getting. If you want me to take a look at it, post a link to your new question here.
2

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:

4 Comments

Thank you!! :) It was driving me crazy!!! Do you have any idea about how I can store directly in an Int [][] array?
Copy the array, don't copy its contents. See @jlordo's answer.
@DimitraMicha that's what I meant by my comment. I believe his answer is better.
Great:) I just don't want to misjudge somebody. But it is good to remember that I have to save the array dynamically everytime:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.