CISC 3115
Modern Programming Techniques
Lecture 08
Collections (Containers), Generics, and Autoboxing

ArrayList

Like all the other collection classes:

ArrayList alist = new ArrayList();		// Object element type

for (int i = 1; i < 10; i++) {
	Integer integer = new Integer(i);	// wrapping primitive int in Integer wrapper object
	alist.add(integer);				// upcast of Integer to Object element type
}

for (int i = 0; i < alist.size(); i++)
	System.out.println(alist.get(i));	// no cast downcast necessary -- calling toString which is defined in Object

for (int i = 0; i < alist.size(); i++) {
	Integer integer = (Integer)alist.get(i));	// downcast of Object to Integer
	int n = integer.intValue();				// extraction of primitive int from Integer wrapper
	n++;									// int operation
	integer = new Integer(n));				// wrapping primitive int in (new) Integer wrapper object
	alist.set(i, integer);					// upcast Integer to Object element type, replacing original element at i
}

Autoboxing and Generics

Overview

As of Java 1.5 (Java 5), two highly useful (and long-awaited) features made their debut

Generics

Type Erasure