Java Reference
In-Depth Information
LISTING 5.11
continued
import java.util.ArrayList;
public class Beatles
{
//-----------------------------------------------------------------
// Stores and modifies a list of band members.
//-----------------------------------------------------------------
public static void main (String[] args)
{
ArrayList<String> band = new ArrayList<String>();
band.add ("Paul");
band.add ("Pete");
band.add ("John");
band.add ("George");
System.out.println (band);
int location = band.indexOf ("Pete");
band.remove (location);
System.out.println (band);
System.out.println ("At index 1: " + band.get(1));
band.add (2, "Ringo");
System.out.println ("Size of the band: " + band.size());
int index = 0;
while (index < band.size())
{
System.out.println (band.get(index));
index++;
}
}
}
OUTPUT
[Paul, Pete, John, George]
[Paul, John, George]
At index 1: John
Size of the band: 4
Paul
John
Ringo
George
 
Search WWH ::




Custom Search