0

So I have to variables in java

List<Integer> a = new arrayList();
List<int[]> b = new arrayList();

When I do a.toArray(new int[a.size()]) it returns an error, but I can do b.toArray(new int[b.size()][]) . I am confused.

Adam
  • 257
  • 3
  • 12
  • Does this answer your question? [How to convert an ArrayList containing Integers to primitive int array?](https://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array) – dnault Oct 28 '20 at 01:05
  • `Integer[] as = a.toArray(new Integer[0]);` works fine. Remember to take out what you put in (autounboxing does not apply here). – Mike 'Pomax' Kamermans Oct 28 '20 at 01:06

2 Answers2

2

You need to specify the correct type.

This should work: a.toArray(new Integer[a.size()]).

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • 1
    And you don't even need `a.size()`, just `0` will do, because `toArray()` just needs to know what type of array it's building, it doesn't use the passed in array for anything else. – Mike 'Pomax' Kamermans Oct 28 '20 at 01:07
  • 3
    @Mike'Pomax'Kamermans Just picking at nits, but... while it's true you *can* pass in an array of any length, _"If the list fits in the specified array, it is returned therein._ Reference: https://docs.oracle.com/javase/8/docs/api/java/util/List.html#toArray-T:A- – dnault Oct 28 '20 at 01:11
  • Right. `toArray(a)` will use `a` if its size is big enough, else it will create a new array of the appropriate size. It is inefficient to create and pass an empty Object (that will be dropped). – cybersam Oct 28 '20 at 01:19
  • 1
    Ah, that's true. I doubt one would _ever_ hit an inefficiency here (if you need efficiency, you're probably not working with code in which you need to even cast ArrayLists to arrays) but not having the new array get thrown away, while a bit of a C++ pattern, is still useful. – Mike 'Pomax' Kamermans Oct 28 '20 at 01:49
2

You seem to be confusing a few things. First a and b aren't even close to being similar things. One is a list that contains Integers. Th second is a list where each member is an array of ints

Regardless of what is in your List<T> the toArray method expects you to pass an array of whatever the element type of your list is (ie. T[]). So for a you need to pass an Integer[]. For b you need to pass an int[][].

So as shown in the other answer you need a.toArray(new Integer[a.size()])

Stealing from the comments on the answer by cybersam, if the array you pass in is of the correct size it'll be used to store the result. Technically you could just use size of 0 and then a new array will be created by the toArray method.

Michael Welch
  • 1,754
  • 2
  • 19
  • 32
  • What is the reason for having to pass in ```Intger[]``` thought? why don't we need to do the same for ```int[][]``` – Adam Oct 28 '20 at 01:15
  • Hey @Adam In the case of `a` the element type is `Integer` so we need to pass an `Integer[]`. For `b` the element type is `int[]` so we need to pass an array of those (in other words an `int[][]`, notice there are two sets of brackets on this one). In either case we are passing an array of whatever the element type was. See the docs for [toArray](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray(T[])) you'll see it takes a `T[]` – Michael Welch Oct 28 '20 at 01:17