Skip to main content
ICT
Lesson A15 - ArrayList
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

B. The ArrayList Class page 4 of 11

  1. a. To declare a reference variable for an ArrayList, do this:

    // myArrayList is a reference to
    // a future ArrayList object
    ArrayList <ClassName> myArrayList;

    An ArrayList is an array of references to objects of type ClassName, where ClassName can be any class defined by you or Java.

    b. To declare a variable and to construct an ArrayList with an unspecified initial capacity, do this:

    // myArrayList is a reference to an ArrayList
    // object. The Java system picks the initial
    // capacity.
    ArrayList <ClassName> myArrayList =
                          new ArrayList <ClassName> ();

    This may not be very efficient. If you have an idea of what size ArrayList you need, start your ArrayList with that capacity.

    c. To declare a variable and to construct an ArrayList with an initial capacity of 15, do this:

    // myArrayList is a reference to an ArrayList
    // object with an initial capacity of 15 elements.
    ArrayList <ClassName> myArrayList =
                          new ArrayList <ClassName> (15);

  2. One way of accessing the elements of an ArrayList is by using an integer index. The index is an integer value that starts at 0 and goes to size()-1.

    To access the object at a particular index, use:

    // Returns the value of the element at index
    Object get(int index);

    System.out.println(myArrayList.get(i));

  3. To add an element to the end of an ArrayList, use:

    // add a reference to an Object to the end of the
    // ArrayList, increasing its size by one
    boolean add(Object obj);

    Program Example 15 - 1:

    import java.util.ArrayList;

    ArrayList <String> names = new ArrayList <String> (10);

    names.add("Cary");
    names.add("Chris");
    names.add("Sandy");
    names.add("Elaine");

    // remove the last element from the list

    String lastOne = names.remove(names.size()-1);
    System.out.println("removed: " + lastOne);
    names.add(2, "Alyce"); // add a name at index 2

    for (int j = 0; j < names.size(); j++)
         System.out.println( j + ": " + names.get(j));

    Run Output:

    removed: Elaine
    0: Cary
    1: Chris
    2: Alyce
    3: Sandy

  4. A shorthand way to iterate through the collection is provided by a “for each” loop. A for each loop starts you at the beginning of the collection and proceeds through all of the elements. It will not allow you to skip elements, add elements or remove elements.

    An example of using it on the collection created in the previous section is

    for(String n : names){
        System.out.println(n);
    }

  5. The add() method adds to the end of an ArrayList. To set the data at a particular index, use:

    // replaces the element at index with
    // objectReference
    Object set(int index, Object obj)

    The index should be within 0 to size-1. The data previously at index is replaced with obj. The element previously at the specified position is returned.

  6. Removing an element from a list: The ArrayList class has a method that will remove an element from the list without leaving a hole in place of the deleted element:

    // Removes the element at index from the list and
    // returns its old value; decrements the indices of
    // the subsequent elements by 1
    Object remove(int index);

    The element at location index will be eliminated. Elements at locations index+1, index+2, …, size()-1 will each be moved down one to fill in the gap.

  7. Inserting an element into an ArrayList at a particular position: When an element is inserted at index, the element previously at index is moved up to index+1, and so on until the element previously at size()-1 is moved up to size(). The size of the ArrayList has now increased by one, and the capacity can be increased again if necessary.

    // Inserts obj before the i-th element; increments
    // the indices of the subsequent elements by 1
    void add(int index, Object obj);


    Inserting is different from setting an element. When set(index, obj) is used, the object reference previously at index is replaced by the new obj. No other elements are affected, and the size does not change.

  8. Whether you are adding at the beginning, middle or end, remember that you are adding an object and must instantiate that object somewhere. Strings hide this fact.

    names.add(2, "Alyce");

    This statement actually creates a String object with the value Alyce.

    If we are using any other object, we must instantiate the object. If we have an ArrayList drawList of DrawingTools, we could add a DrawingTool in the following way.

    drawList.add(new DrawingTool());

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.