-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayLists.java
More file actions
38 lines (27 loc) · 1.06 KB
/
ArrayLists.java
File metadata and controls
38 lines (27 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.ArrayList;
public class ArrayLists {
public static void main(String[] args) {
//ArrayList = a resizable array;
// Elements can be added and removed after the compilation phase
// Store reference data types IE Integer, String, ETC
//list reference data type within angle brackets <>
//if you need to store primitive data types, use wrapper classes
ArrayList<String> food = new ArrayList<String>();
//Use the add method to add items to the ArrayList
food.add("Pizza");
food.add("Hamburger");
food.add("Hotdog");
//The set method changes the index of the corresponding value with the item (index, item)
food.set(0, "sushi");
//removes the item from the specified index [2]
food.remove(2);
// method for clearing the Arraylist
// food.clear();
//Displaying elements of the ArrayList
//ArrayLists use size() instead of length(), which arrays use
for(int i = 0; i<food.size(); i++) {
//the get method gets the item from the corresponding index
System.out.println(food.get(i));
}
}
}