61 ArrayList
62 arrayList.add("1");
63 arrayList.add("2");
64 arrayList.add("3");
65 arrayList.add("4");
66 arrayList.add("5");
67 List
68 for (int i = 0; i < lst.size(); i++)
69 System.out.println(lst.get(i));
70 // remove one element from sub list
71 String obj = lst.remove(0);
72 System.out.println(obj + " is removed");
73 for (String str: arrayList)
74 System.out.println(str);
75 //get object array with normal method
76 Object[] objArray = arrayList.toArray();
77 for (Object obj1 : objArray)
78 System.out.println(obj1);
79 //get object array with generic method
80 String[] strArray = arrayList.toArray(new String[0]);
81 for (String str : strArray)
82 System.out.println(str);
83 }
84
85 public static void showListIterator() {
86 ArrayList
87 aList.add("1");
88 aList.add("2");
89 aList.add("3");
90 aList.add("4");
91 aList.add("5");
92
93 ListIterator
94 while (listIterator.hasNext()) {
95 System.out.println(listIterator.next());
96 System.out.println("Previous: " + listIterator.previousIndex());
97 System.out.println("Next: " + listIterator.nextIndex());
98 }
99 while (listIterator.hasPrevious()) {
100 System.out.println(listIterator.previous());
101 System.out.println("Previous: " + listIterator.previousIndex());
102 System.out.println("Next: " + listIterator.nextIndex());
103 }
104 listIterator = aList.listIterator(2);
105 listIterator.next();
106 listIterator.set("100");
107 listIterator.next();
108 listIterator.remove();
109 for (String str : aList)
110 System.out.println(str);
111
112 if (aList.contains("4"))
113 System.out.println("True");
114 else
115 System.out.println("False");
116 }
117
118 public static void showFillAndReplace() {
119 ArrayList
120 arrayList.add("A");
121 arrayList.add("B");
122 arrayList.add("A");
123 arrayList.add("C");
124 arrayList.add("D");
125 Collections.replaceAll(arrayList, "A", "Replace All");
126 System.out.println(arrayList);
127 Collections.fill(arrayList, "REPLACED");
128 System.out.println(arrayList);
129 }
130
131 public static void showCollectionOperation() {
132 List
133 colours.add("red");
134 colours.add("green");
135 colours.add("blue");
136
137 System.out.println(colours);
138 Collections.swap(colours, 0, 2);
139 System.out.println(colours);
140
141 Collections.reverse(colours);
142 System.out.println(colours);
143
144 Collections.so