Sunday, 11 May 2014

LinkedList : add : Add element to the End of List

public boolean add(E e)
Appends the specified element to the end of this list. Returns true if the element added successfully.

import java.util.*;

class LinkedListAdd{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  for(int i=0; i<5; i++){
   System.out.print("is element " + i + " added ");
   System.out.println(myList.add(i));
   System.out.println("Elements in myList are");
   System.out.println(myList+"\n");
  }
 }
}

Output
is element 0 added true
Elements in myList are
[0]

is element 1 added true
Elements in myList are
[0, 1]

is element 2 added true
Elements in myList are
[0, 1, 2]

is element 3 added true
Elements in myList are
[0, 1, 2, 3]

is element 4 added true
Elements in myList are
[0, 1, 2, 3, 4]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment