Friday, May 29, 2009

Using generic ArrayList

JDK 5.0 provides typed array list. Before JDK 5.0 Array List store the objects of type Object. But now type of the elements of an ArrayList can be specified.

Syntax:
ArrayList list = new ArrayList();

Example:

import java.util.ArrayList;

public class MyArrayList {

public static void main(String[] args) {

Seller sel = new Seller();
Buyer buy = new Buyer();

// declare an array list of type Seller
ArrayList list = new ArrayList();
list.add(sel); // add object of type Seller which is permisible
list.add(buy); // this statement will give an error
}
}


source: java-tips.org

No comments:

Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory