Populating and Updating a LinkedList


It is best to use an ArrayList when: On the other end, it is best to use a LinkedList when:
LinkList.java (LinkedList being reserved)
 // Java program to populate and update a LinkedList
 import java.util.LinkedList;

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

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

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

(index to update)   (number to update)        




      It is always darkest before the dawn.