1

I have an ArrayList< int[] > and would like to convert this to an int[][]. The resulting array has the same int[] for each entry even though they are distinct (yes, I've checked!). Am I missing something simple here? The length of the arrays in int[][] is given by arr.length

int[][] vals = new int[ list.size() ][ arr.length ];
list.toArray( vals );

EDIT: I've realized that the code works ok, so heres a larger sample of the code where the problem is arising. permute() does as it says and permutes the integers in the given array returning true when a new permutation is done.

List< int[] > list = new ArrayList<>();
do {
    list.add( vals );
} while ( permute( vals ) ); // I print vals here and the permutations are all unique each time
int[][] permutations = new int[ list.size() ][];
list.toArray( permutations );
// If I print permutations now, all the arrays inside it are the same
2
  • 1
    The above code seems to work for me. A larger example might be needed to help figure out why it isn't working for you. (FYI, the arr.length isn't necessary, and it doesn't really matter what you put in there because the elements of the outer array are references that will get overwritten anyway. You can just make it [].) Commented Oct 8, 2013 at 22:31
  • I've realized that...see the edit with the larger code Commented Oct 8, 2013 at 22:57

3 Answers 3

2
int[][] vals = new int[ list.size() ][ arr.length ];

for(int i = 0; i < list.size(); i++) {
   vals[i] = list.get(i);
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's not where the problem is.
@TomHawtin-tackline FYI, this answer was posted before the OP provided additional information in the edit.
2

To answer the new question: You don't show the declaration of vals, but assuming it's declared as int[], vals is actually a reference to an array. So every time you do your list.add, you're adding the same reference to the list. I assume permute(vals) changes the values in the list, but it won't change the reference, which means that at the end of the loop, every item in list is the same reference to the same array with the same values. What you'll need to do is make a copy of the array. Try this:

list.add( Arrays.copyOf (vals, vals.length) );

3 Comments

I'll try that. How come when I print vals then I get the different values each time?
Are you printing them inside the do..while loop? Yes, the values inside vals do change. But since vals is a reference and you were adding the same reference as each list item, every time the values inside vals changed, that means that every list item, which pointed to the same array, would also have values that changed, because list is storing a reference to vals and not the actual values inside vals.
list.add(vals.clone()); would be cleaner.
0

Read the docs carefully http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#toArray%28%29

You need

vals = list.toArray(vals);

I don't know why it does not work for some of you - it works for me very well

5 Comments

That returns an Object[].
Actually, if you do it this way, the returned array will be an Object[] not an int[][].
@arshajii @StephenC list.toArray(vals); returns correct type
OK, the edit now results in code that actually compiles, which is an improvement, but it's still no different from the original post. list.toArray(vals) will put the values in vals as long as vals is large enough to hold them. It creates a new array only if the argument is too short. And in the OP's code, it wasn't too short. So assigning vals to the result, in this case, makes no difference.
OK, my bad. I prefer to USE parameter(s) and return result(s). I did not read the docs completely myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.