Java和C++在细节上的差异(四)(六)

2014-11-24 02:04:05 · 作者: · 浏览: 6
ListIterator listIterator = aList.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 publicstaticvoidshowFillAndReplace() {
119 ArrayList arrayList = newArrayList();
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 publicstaticvoidshowCollectionOperation() {
132 List colours = newArrayList();
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.sort(colours);
145 System.out.println(Arrays.toString(colours.toArray()));
146 Collections.sort(colours, Collections.reverseOrder());
147 System.out.println(Arrays.toString(colours.toArray()));
148
149 intindex = Collections.binarySearch(colours, "green");
150 System.out.println("Element found at : " + index);
151 ArrayList arrayList = newArrayList();
152 arrayList.add(newInteger("3"));
153 arrayList.add(newInteger("1"));
154 arrayList.add(newInteger("8"));
155 arrayList.add(newInteger("3"));
156 arrayList.add(newInteger("5"));
157 System.out.println(Collections.min(arrayList));
158 System.out.println(Collections.max(arrayList));
159 }
160
161 publicstaticvoidshowMinMax() {
162 ArrayList arrayList = newArrayList();
163 arrayList.add(newInteger("3"));
164 arrayList.add(newInteger("1"));
165 arrayList.add(newInteger("8"));
166 arrayList.add(newInteger("3"));
167 arrayList.add(newInteger("5"));
168 System.out.println(Collections.min(arrayList));
169 System.out.println(Collections.max(arrayList));
170 }
171
172 publicstaticvoidshowSynchronizedList() {