I'm trying to initialize an ArrayListto use later in my code, but it seems like it doesn't accept doubles.
public ArrayList<double> list = new ArrayList<double>();
It gives an error under 'double', sayin "Syntax error on token "double", Dimensions expected after this token"
24 Answers
An ArrayList doesn't take raw data types (ie, double). Use the wrapper class (ie, Double) instead :
public ArrayList<Double> list = new ArrayList<>(); Also, as of Java 7, no need to specify that it's for the Double class, it will figure it out automatically, so you can just specify <> for the ArrayList.
You need to use the Wrapper class for double which is Double. Try
public ArrayList<Double> list = new ArrayList<Double>(); 0In Java, ArrayLists (and other generic classes) only accept object references as types, not primitive data types. There are wrapper classes that allow you to emulate using primitives, though: Boolean, Byte, Short, Character, Integer, Long, Float and Double;
public ArrayList<Double> list = new ArrayList<Double>(); //or "public ArrayList<Double> list = new ArrayList<>();" in Java 1.7 and beyond Values inside are "autoboxed" and "autounboxed" so you can treat doubles as Doubles without problems, and vice versa. You may need to explicitly specify whether you want arguments to be treated as int or Integer when dealing with lists of integral types, though, to disambiguate between cases like remove(int index) and remove(Object o).
public ArrayList<Double> doubleList = new ArrayList<>(); From Java 1.7, you don't need to write Double while initializing ArrayList, so it's your choice to write Double in new ArrayList<>(); or new ArrayList<Double>(); or not.. otherwise it's not compulsory.
Also known as a dynamic array.
No need to pre-determine the number of elements up front, just add to the array as we need it