1

I have an ArrayList created like this:

public class Main {

    public static void main(String[] args) {
        // write your code here
        ArrayList list1 = new ArrayList();

        list1.add(0, 5);
        list1.add(1, 3.5);
        list1.add(2, 10);

    }
}

I am trying to create an array from it, using the toArray method:

public class Main {

    public static void main(String[] args) {
        // write your code here
        ArrayList list1 = new ArrayList();

        list1.add(0, 5);
        list1.add(1, 3.5);
        list1.add(2, 10);

        Double[] list2 = list1.toArray(new Double[list1.size()]);
    }
}

However, I am getting an error:

(Error:(16, 39) java: incompatible types: java.lang.Object[] ).

So I tried to cast the right side to double:

Double[] list2 = (Double[]) list1.toArray(new Double[list1.size()])

This time i am getting Exception in thread "main". I also tried to declare my ArrayList as double from beginning:

ArrayList<double> list1 = new ArrayList()<double>

With no success. How to do it properly? I know that my problem is probably something very basic.

user85421
  • 28,957
  • 10
  • 64
  • 87
s19
  • 13
  • 3
  • https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java?rq=1 – Muthusamy Dec 07 '19 at 11:17
  • 1
    The correct syntax is `ArrayList list = new ArrayList<>();` – Minn Dec 07 '19 at 11:18
  • You can`t put primitive type into List or any Collection. Generic classes only work with objects and don't support primitives. Use wrapper class [instead](https://www.baeldung.com/java-wrapper-classes). – Vlad Zaichyk Dec 07 '19 at 11:21
  • @StephenC Actually it will solve the problem, I just tested this. Ideally, you would call toArray with `new Double[0]` but this should work fine in this case. – Minn Dec 07 '19 at 11:28
  • 1
    @Minn Only that change wouldn't let above code to compile. You still need to change `list1.add(0, 5);` to `list1.add(0, 5.0);` otherwise to place `5` to internal `Object[]` array it would be wrapped in `Intger` object which can't be placed in `Double[]` array. – Pshemo Dec 07 '19 at 11:31
  • @Pshemo yes that is indeed correct. I was just pointing out that the usage of `toArray` is not the problem. – Minn Dec 07 '19 at 11:33
  • I am still not getting it. I tried the String[] array = list.toArray(new String[0]) solution, but id does not work form my list. – s19 Dec 07 '19 at 11:39

1 Answers1

3

The problem is that you are doing a number of things wrong:

  1. ArrayList list1 = new ArrayList(); is incorrect because you are using a raw type. You should have gotten a compiler warning for that.

  2. Given the previous list1.add(0, 5) is incorrect. The 5 will be boxed as an Integer because that compiler doesn't know that the list is only supposed to contain Double values.

  3. You were getting this:

    (Error:(16, 39) java: incompatible types: java.lang.Object[] ).
    

    because you must have done something like this:

    Double[] list2 = list1.toArray();
    

    You appear to have corrected that in the code that you posted. But the no-args toArray method returns an Object[] containing the list content.

  4. ArrayList<double> list1 = new ArrayList()<double> is incorrect because you cannot use a primitive type as a generic type parameter, and because the syntax on the RHS is wrong.

    The correct version is ArrayList<Double> list1 = new ArrayList<>(); with an empty diamond.

Surprisingly, the best (most efficient) way to code the toArray is:

Double[] list2 = list1.toArray(new Double[0]);

Apparently, the implementation is able to initialize the array faster if it allocates it itself. (Or so I have heard ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216