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.