Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize ArrayList with doubles

Tags:

java

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"

like image 786
R_vg Avatar asked May 08 '14 08:05

R_vg


People also ask

Can you make an ArrayList of doubles?

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.

What does ArrayList double mean?

Look at this: ArrayList<Double> list = new ArrayList<Double>(); it creates a list that can only contain double values. A double is a variable type that can contain whole numbers and decimal numbers. So creating a list like this only allows the program to add numbers to the list: list.


2 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.

like image 179
AntonH Avatar answered Sep 28 '22 04:09

AntonH


You need to use the Wrapper class for double which is Double. Try

public ArrayList<Double> list = new ArrayList<Double>();
like image 23
anirudh Avatar answered Sep 28 '22 05:09

anirudh