public class ArrayListBasics
1: import java.util.List;
2: import java.util.ArrayList;
3: import java.util.LinkedList;
4:
5: /**
6: * Create and use some ArrayList objects (and maybe a LinkedList, too)
7: *
8: * @author Mark Young (A00000000)
9: */
10: public class ArrayListBasics {
11:
12: /**
13: * Do it all.
14: *
15: * @param args the command line arguments (ignored)
16: */
17: public static void main(String[] args) {
18: // create a list object
19: List<String> myWords = new LinkedList<>();
20: List<Student> myClass = new ArrayList<>();
21:
22: // create some other list objects using "wrapper" classes
23: List<Integer> myInts = new ArrayList<>();
24: List<Double> myDbls = new ArrayList<>();
25: List<Double> myOtherDbls = new LinkedList<>();
26:
27: // add some values to them (on the end)
28: myWords.add("ten");
29: myWords.add("twenty");
30: myWords.add("thirty");
31: myWords.add("forty");
32: myWords.add("25");
33: myClass.add(new Student("Bob"));
34: myClass.add(new Student("Carol"));
35: myInts.add(10);
36: myInts.add(20);
37: myInts.add(30);
38: myDbls.add(10.0);
39: myDbls.add(20.0);
40: myDbls.add(1, 15.0);
41:
42: // FAIL to add values of the wrong type
43: // myDbls.add("thirty");
44:
45: // print them out
46: System.out.println(myWords);
47: System.out.println(myClass);
48: System.out.println(myInts);
49: System.out.println(myDbls);
50:
51: // look at some of the elements
52: String word0 = myWords.get(0);
53: System.out.println("The 1st word in " + myWords
54: + " is " + word0);
55: System.out.println("The 3rd word in " + myWords
56: + " is " + myWords.get(3 - 1));
57:
58: // how long is that list?
59: System.out.println("There are " + myWords.size()
60: + " elements in " + myWords);
61:
62: // add some values (not on the end)
63: myWords.add(1, "fifteen");
64: System.out.println("The 3rd word in " + myWords
65: + " is " + myWords.get(3 - 1));
66:
67: // check if it has certain values on it
68: if (myWords.contains("ten")) {
69: System.out.println(myWords + " contains a ten");
70: } else {
71: System.out.println(myWords + " DOESN'T contains a ten");
72: }
73: if (myWords.contains("hundred")) {
74: System.out.println(myWords + " contains a hundred");
75: } else {
76: System.out.println(myWords + " DOESN'T contains a hundred");
77: }
78:
79: // check where those values are
80: System.out.println("The word \"twenty\" appears at position "
81: + myWords.indexOf("twenty") + " in " + myWords);
82: System.out.println("The word \"hundred\" appears at position "
83: + myWords.indexOf("hundred") + " in " + myWords);
84:
85: // remove by position
86: myWords.remove(0);
87: myWords.remove(3);
88: System.out.println("After removing 0 and 3 (in that order "
89: + "the words list is " + myWords);
90:
91: // remove by value
92: myWords.remove("twenty");
93: myDbls.remove(20.0);
94: System.out.println("After removing \"twenty\" "
95: + "the words list is " + myWords);
96: System.out.println("After removing 20.0 "
97: + "the doubles list is " + myDbls);
98:
99: // remove by value in a list of Integers
100: myInts.remove(Integer.valueOf(10));
101: System.out.println("After removing 10 "
102: + "the integers list is " + myInts);
103:
104: // check if a list is empty
105: if (myWords.isEmpty()) {
106: System.out.println(myWords + " is empty!");
107: } else {
108: System.out.println(myWords + " is NOT empty!");
109: }
110:
111: // make a list empty (clear it out)
112: myWords.clear();
113: if (myWords.isEmpty()) {
114: System.out.println(myWords + " is empty!");
115: } else {
116: System.out.println(myWords + " is NOT empty!");
117: }
118: }
119:
120: }