Aggregation (Cont.)

List<Student> cs_students = new ArrayList<Student>( );
The java.util.List is a child interface of Collection. It is an ordered collection of objects. Since List preserves the insertion order, it allows positional access and insertion of elements.

List interface is implemented by ArrayList, LinkedList, Vector, and Stack classes.

List is an interface, and the instances of List can be created in the following ways:
List a = new ArrayList( );
List b = new LinkedList( );
List c = new Vector( ); 
List d = new Stack( );

Generics is to restrict the type of object that can be stored in the List.

The type-safe List can be defined in the following way:
// Obj is the type of object to 
// be stored in List.
List<Obj> list = new ArrayList<Obj>( );
cs_students.add( s1 );
List interface extends Collection, hence it supports all the operations of Collection interface, along with the additional operations: add, remove, get, and set operations such as

    void add(int index, Object O): This method adds given element at specified index.
for ( Student s : students )
The for loop is used to traverse unconditionally to all of the list items. Other methods to iterate a list include (i) the classic array index loop such as for (int i = 0; i < collection.length; i++), and (ii) the Iterator hasNext() / next()



      “You do not write your life with words...    
      You write it with actions.    
      What you think is not important.    
      It is only important what you do.”    
      ― Patrick Ness, A Monster Calls