Friday, March 4, 2011

How do I use ArrayList class?

import java.util.ArrayList;
import java.util.List;

public class ArrayListExample
{
public static void main(String[] args)
{
List list = new ArrayList();

// adding items into array list
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");

// remove the third item from array list
list.remove(2);

// iterate array list item and print it
for (int i = 0; i < list.size(); i++)
{
String item = (String) list.get(i);
System.out.println("Item = " + item);
}

Object[] array = list.toArray();
}
}

No comments:

Post a Comment