University of Cincinnati logo and link  
ArrayLists Java
 
  UC ingot 
  • We've used arrays before...

  • String[] names = new String[25];
    • But with an array, we have to specify its size ahead of time.  This limits flexibility!
  • Fortunately, Java gives us an alternative, the ArrayList.
    • ArrayList is a Java class designed to hold a group of objects.
    • It automatically resizes to hold the number of objects you give it.
    • Like Arrays, each element in an ArrayList has a position, or index.
    • Before ArrayLists, we used Vectors, which are very similar. 
    • Before generics, ArrayLists hold objects of class Object, the supeclass of all classes.
      • So, to use ArrayLists, we usually need to cast these objects to a more usable form once we retrieve them.
      • But, with generics, we can specify the type of object we are storing in the ArrayList by specifying the type in angle brackets.  For instance:
           ArrayList<Integer> numberList = new ArrayList()<Integer>;
                          
                             Will store Integers in an ArrayList.
                            As an added bonus, generics gives us autoboxing and autounboxing, which means we can use this same ArrayList to store values of the int primitive.