Many programs need to store some kind of collection of objects. The type of object in the collection may change, but the collection is similar: we add objects to it, we remove objects from it, we search it, and so on. We can use the code that implements the collection with many different types of objects by making the collection generic.
A generic collection class has a type parameter which specifies the type of object in the collection. By substituting different types for this parameter (Strings, Students, Rectangles,...) we can use the collection to hold different types of objects in different programs.
java.util.ArrayList is a generic class for resizable arrays. It has a type parameter for the type of object that will be stored in the array. The ArrayList class is defined as
public class ArrayList<E> ...
To create an ArrayList, you must substitute E with the type of objects you will store in the array.
Many of the methods have the same type parameter for manipulating the objects in the array. When you use these methods you must use an object of the same type that was used when you created the ArrayList. For example
public boolean add(E element) ... public boolean add(int index, E element) ... public E remove(int index) ... public boolean remove(E element) ... public boolean contains(E element) ...
You can look up the public methods available for the ArrayList class at http://java.sun.com/javase/8/docs/api/java/util/ArrayList.html
import java.util.ArrayList; public class ArrayListStrEx { public static void main(String[] args) { int i; String word; // create an arraylist of strings with 4 elements // and store 4 words in the arraylist (each stored at end) ArrayList<String> wordlist = new ArrayList<String>(4); wordlist.add("first"); wordlist.add("another"); wordlist.add("music"); wordlist.add("Tuesday"); // add 2 more words, then print the list wordlist.add(1,"ADDED"); wordlist.add(4,"dark"); System.out.println("\nThe list:"); for (i = 0 ; i < wordlist.size() ; i++) System.out.println(wordlist.get(i)); // remove the word "another" if it is in wordlist if (wordlist.remove("another")) System.out.println("\nanother removed"); else System.out.println("\nanother not found"); // remove the first element word = wordlist.remove(0); System.out.println(word + " removed"); // print the list again System.out.println("\nThe list:"); for (i = 0 ; i < wordlist.size() ; i++) System.out.println(wordlist.get(i)); } }
The list: first ADDED another music dark Tuesday another removed first removed The list: ADDED music dark Tuesday
import java.util.ArrayList; public class ArrayListIntEx { public static void main(String[] args) { int i; Integer inum; // create an arraylist of strings with 4 elements // and store 4 Integers in the arraylist (each stored at end) ArrayList<Integer> numlist = new ArrayList<Integer>(4); numlist.add(new Integer(6)); numlist.add(new Integer(21)); numlist.add(new Integer(13)); numlist.add(new Integer(17)); // add 2 more Integers, then print the list numlist.add(new Integer(24)); numlist.add(new Integer(14)); System.out.println("\nThe list:"); for (i = 0 ; i < numlist.size() ; i++) System.out.println(numlist.get(i).intValue()); // remove the number 24 if it is in numlist inum = new Integer(24); if (numlist.remove(inum)) System.out.println("\n24 removed"); else System.out.println("\n24 not found"); // remove the first element inum = numlist.remove(0); System.out.println(inum.intValue() + " removed"); // print the list again System.out.println("\nThe list:"); for (i = 0 ; i < numlist.size() ; i++) System.out.println(numlist.get(i).intValue()); } }
The list: 6 21 13 17 24 14 24 removed 6 removed The list: 21 13 17 14
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2021