Tuesday 27 May 2014

ArrayList : ArrayList(Collection c)

public ArrayList(Collection<? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

import java.util.*;

class ArrayListConstructor3{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  HashSet<Integer> mySet;
  
  mySet = new HashSet<> (); 
  
  /* Add Elements to mySet */
  mySet.add(10);
  mySet.add(20);
  
  myList = new ArrayList<> (mySet);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[20, 10]

1. Throws NullPointerException if the specified collection is null

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment