package Program;
import java.util.*;
class Main
{
public static void main(String[] args)
{
// Create an ArrayList with a capacity for 10 elements.
ArrayList arrList = new ArrayList(10);
// Add three elements to the ArrayList.
arrList.add(new Integer(1));
arrList.add(new Integer(2));
arrList.add(new Integer(3));
// Print the size of the ArrayList.
int size = arrList.size();
System.out.println("arrList contains " + size +
" elements.");
// Determine if the ArrayList contains an element.
if (arrList.contains(new Integer(4)))
{
System.out.println("There is already an Integer with " +
"the value 4 in the ArrayList.");
}
else
{
System.out.println("There is no Integer with " +
"the value 4 in the ArrayList.");
}
// Change the value of the second element in the ArrayList.
arrList.set(1, new Integer(4));
// Print out the elements without using an iterator.
for (int i = 0; i < arrList.size(); i++)
{
System.out.println(arrList.get(i));
}
// Remove the second element in the ArrayList.
Object o = arrList.remove(1);
System.out.println("Removed " + o.toString());
// If the ArrayList is not empty, clear it out now.
if (!arrList.isEmpty())
{
arrList.clear();
System.out.println("clear");
}
}
}
Output:
arrList contains 3 elements.
There is no Integer with the value 4 in the ArrayList.
1
4
3
Removed 4
clear
Comments
Post a Comment