TestArrayList.java
Select all
/* TestArrayList.java David Klick CIS 260 3/23/09 This program demonstrates a few features of the ArrayList class. Note that the datatypes could easily have been reversed within the program so that we were looking up Integers based on String values. */ import java.util.*; public class TestArrayList { public static void main(String[] args) { ArrayList
alist = new ArrayList
(); // you can add and remove items dynamically alist.add(17); alist.add(26); alist.add(-8); alist.add(142); alist.remove(1); // removes item at index for (int i : alist) System.out.print(i + " "); System.out.println(); // you can clear out an ArrayList alist.clear(); // if you keep track of object references, you can // check for and remove specific objects ArrayList
slist = new ArrayList
(); slist.add("C"); slist.add("I"); slist.add("S"); slist.add("2"); slist.add("6"); slist.add("0"); System.out.println(slist.contains("2")); // true System.out.println(slist.indexOf("2")); // 3 slist.remove("2"); System.out.println(slist.indexOf("2")); // -1 (not found) for (String s : slist) System.out.print(s + " "); System.out.println(); } } /* Output: 17 -8 142 true 3 -1 C I S 6 0 */