Friday, 13 January 2017

ArrayListWithConstructor


package arraylist;

import java.util.*;
public class ArrayListWithConstructor {

String name;
ArrayListWithConstructor(String name)
{
this.name = name;
System.out.println(name);

}

public static void main(String[] args) {
// TODO Auto-generated method stub

ArrayListWithConstructor ta1 = new ArrayListWithConstructor("One");
ArrayListWithConstructor ta2 = new ArrayListWithConstructor("Two");
ArrayListWithConstructor ta3 = new ArrayListWithConstructor("Three");
ArrayListWithConstructor ta4 = new ArrayListWithConstructor("Four");

ArrayList<Object> ar1 = new ArrayList<>();
ar1.add(ta1);
ar1.add(ta2);
ar1.add(ta3);
ar1.add(ta4);

Iterator itr = ar1.iterator();
while(itr.hasNext())
{
ArrayListWithConstructor ta = (ArrayListWithConstructor)itr.next();
System.out.println(ta.name);
}

}
}

No comments:

Post a Comment