Initialize ArrayList with Pairs

I try to initialize ArrayListto later in my code, but it looks like it does not accept doubling.

public ArrayList<double> list = new ArrayList<double>();

It gives an error in "double", sayin "Syntax error on the token" double ", the sizes expected after this token"

+4
source share
4 answers

An ArrayListdoes not use raw data types (i.e. double). Instead, use a wrapper class (i.e. double):

public ArrayList<Double> list = new ArrayList<>();

In addition, as with Java 7, there is no need to specify it for the class double, it will automatically figure it out, so you can just point it <>to an ArrayList.

+12
source

Wrapper class double, double.

public ArrayList<Double> list = new ArrayList<Double>();
+5

Java ArrayLists ( ) , . -, : Boolean, Byte, Short, Character, Integer, Long, Float Double;

public ArrayList<Double> list = new ArrayList<Double>(); 
//or "public ArrayList<Double> list = new ArrayList<>();" in Java 1.7 and beyond

"autoboxed" "autounboxed", doubles doubles . , , , int Integer , , , remove(int index) remove(Object o).

+3
public ArrayList<Double> doubleList = new ArrayList<>();

From Java, 1.7you do not need to write Double while initializing ArrayList, so you can write Double in new ArrayList<>(); or new ArrayList<Double>();or not .. otherwise it is not necessary.

Also known as dynamic array.

There is no need to pre-determine the number of elements up front, just add to the array as we need it

+1
source

Source: https://habr.com/ru/post/1539721/


All Articles