Friday, 30 May 2014

ArrayList : add(E e) : Append Element

public boolean add(E e)
Appends the specified element to the end of this list. 

import java.util.*;

class ArrayListAdd{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  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