forked from Nnamdikeshi/Java2545examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddingToArrayList.java
More file actions
42 lines (25 loc) · 1.26 KB
/
Copy pathAddingToArrayList.java
File metadata and controls
42 lines (25 loc) · 1.26 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
39
40
41
package week4_data_structures;
import java.util.ArrayList;
public class AddingToArrayList {
public static void main(String[] args) {
ArrayList cities = new ArrayList();
cities.add("Minneapolis");
cities.add("St. Paul");
cities.add("Brooklyn Park");
cities.add("Bloomington");
cities.add(2, "Roseville"); //Insert this at position 2, other elements shift up
System.out.println(cities);
// Prints [Minneapolis, St. Paul, Roseville, Brooklyn Park, Bloomington]
cities.add(3, "Richfield");
System.out.println(cities);
// Now the list is [Minneapolis, St. Paul, Roseville, Richfield, Brooklyn Park, Bloomington]
//Add to the start of the list - add in position 0, all other elements shift up
cities.add(0, "Eagan");
System.out.println(cities);
// Now the list is [Eagan, Minneapolis, St. Paul, Roseville, Richfield, Brooklyn Park, Bloomington]
// Adding to the end is easy - use add(newElement) - by default new items are added to the end of the list
cities.add("Shakopee");
System.out.println(cities);
// Now the list is [Eagan, Minneapolis, St. Paul, Roseville, Richfield, Brooklyn Park, Bloomington, Shakopee]
}
}