Populating and Updating an ArrayList


The LinkedList class is a collection which can contain many objects of the same type, just like the ArrayList. The LinkedList class has all of the same methods as the ArrayList class because they both implement the List interface. This means that you can add items, change items, remove items and clear the list in the same way. However, while the ArrayList class and the LinkedList class can be used in the same way, they are built very differently.
AList.java (ArrayList being reserved)

 // Java program to populate and update an ArrayList
 import java.util.ArrayList;

 class AList {
   // The driver method
   public static void main( String[ ] args ) {
     ArrayList<Integer> numbers = new ArrayList<>( );
     int n     = args.length;
     int index = Integer.parseInt( args[n-2] );
     int value = Integer.parseInt( args[n-1] );

     // Add elements in the array list.
     for ( int i = 0; i < n-2; i++ )
       numbers.( Integer.parseInt( args[i] ) );
     System.out.println( "ArrayList: " + numbers );

     // Change an element.
     numbers.( index, value );
     System.out.println( "Updated ArrayList: " + numbers );
   }
 }
shell> java AList  (a list of integers to be inserted)

(index to update)   (number to update)        




      Q: Why can’t a bike stand by itself?    
      A: Because it is two tired.