Define a public class ArrayListTest which has a no-argument constructor and complete the following tasks in the constructor ➢ Create an ArrayList object and call add method to add some names James Tom
Here's one possible implementation:
public class ArrayListTest {
public ArrayListTest() {
ArrayList
// output all names using index-based loop
for (int count = 0; count < list.size(); count++) {
System.out.print(list.get(count) + " ");
}
System.out.println();
// output all names using iterator and remove Tom
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
String name = iter.next();
System.out.print(name + " ");
if (name.equals("Tom")) {
iter.remove();
}
}
System.out.println();
// sort the names in ascending order
Collections.sort(list);
// output all names using for-each loop
for (String name : list) {
System.out.print(name + " ");
}
System.out.println();
}
public static void main(String[] args) {
ArrayListTest test = new ArrayListTest();
}
原文地址: https://www.cveoy.top/t/topic/fDGR 著作权归作者所有。请勿转载和采集!