Tuesday 25 March 2014

size() : Number Of Elements in the List

int size()
    Returns the number of elements in this list.

import java.util.*;
class ListSize{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  System.out.println("Number Of Elements in myList is " + myList.size());
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(-100);
  myList.add(null);
  myList.add(20);
  myList.add(null);
  
  System.out.println("Number Of Elements in myList is " + myList.size());
  System.out.println("Elements in myList are");
  System.out.println(myList);
 }
}

Output

Number Of Elements in myList is 0
Number Of Elements in myList is 6
Elements in myList are
[10, 20, -100, null, 20, null]

As You observe the output, myList is of type ArrayList, which is allowing duplicates and multiple null values also.

The return type of the size is int, so If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment