The ArrayList Class




What kind of element can be stored in an ArrayList?

To store a primitive data type in an ArrayList we must "wrap" it in an Object by using one of the 8 "type-wrapper" classes:

  1. Integer
  2. Double
  3. Byte
  4. Short
  5. Float
  6. Long
  7. Character
  8. Boolean

Sample Program

import java.util.*; public class TestArrayList { public static void main(String args[]) { ArrayList list = new ArrayList(); list.add(new Integer(45)); list.add(new Double(23.5)); list.add(new Boolean(true)); list.add(new Character('A')); for (int i = 0; i < list.size(); i++) System.out.println(list.get(i)); } } /* Output: csc% java TestArrayList 45 23.5 true A */