Java Reference
In-Depth Information
Figure 4
Removing an Element from the Middle of an Array List
After each call to the add method, the size of the array list increases by 1 (see
Figure
3
).
Conversely, the call
accounts.remove(i)
removes the element at position
i
,
moves all elements after the removed element down by one position, and reduces the
size of the array list by 1 (see
Figure 4
).
The following program demonstrates the methods of the
ArrayList
class. Note
that you import the generic class
java.util.ArrayList
, without the type
parameter.
ch07/arraylist/ArrayListTester.java
1
import
java.util.ArrayList;
2
3 /**
4
This program tests the ArrayList class.
5 */
6
public class
ArrayListTester
7 {
8
public static void
main(String[] args)
9 {
10 ArrayList<BankAccount> accounts
11 =
new
ArrayList<BankAccount>();
12 accounts.add(
new
BankAccount(
1001
));
13 accounts.add(
new
BankAccount(
1015
));
294
295