📅  最后修改于: 2023-12-03 15:15:55.285000             🧑  作者: Mango
In Java, the ArrayList class is a resizable array that can store objects. An ArrayList can store any type of objects including Double.
To create an ArrayList of Double in Java, we can use the following syntax:
ArrayList<Double> arrayList = new ArrayList<Double>();
Here, the ArrayList is initialized with a capacity of 10. The capacity can be increased dynamically as per the requirement.
To add an element in the ArrayList, we can use the add
method. For example:
arrayList.add(3.14159);
arrayList.add(2.71828);
arrayList.add(1.41421);
To access the elements of the ArrayList, we can use the get
method. For example:
Double pi = arrayList.get(0);
Double e = arrayList.get(1);
Double sqrt2 = arrayList.get(2);
To remove an element from the ArrayList, we can use the remove
method. For example:
arrayList.remove(1); // removes the element at index 1
To iterate over the elements of the ArrayList, we can use a for-each loop. For example:
for(Double number : arrayList) {
System.out.println(number);
}
This will print all the elements of the ArrayList.
That's all about creating and using a Java ArrayList with Double.