Java Reference
In-Depth Information
1
// Fig. 21.5: ListTest.java
2
// ListTest class to demonstrate List capabilities.
3
import
com.deitel.datastructures.List;
4
import
com.deitel.datastructures.EmptyListException;
5
6
public class
ListTest
7
{
8
public static void
main(String[] args)
9
{
10
List<Integer> list =
new
List<>();
11
12
// insert integers in list
list.insertAtFront(
-1
);
list.print();
list.insertAtFront(
0
);
list.print();
list.insertAtBack(
1
);
list.print();
list.insertAtBack(
5
);
list.print();
13
14
15
16
17
18
19
20
21
22
// remove objects from list; print after each removal
23
try
24
{
25
int
removedItem = list.removeFromFront();
26
System.out.printf(
"%n%d removed%n"
, removedItem);
27
list.print();
28
29
removedItem = list.removeFromFront();
30
System.out.printf(
"%n%d removed%n"
, removedItem);
31
list.print();
32
33
removedItem = list.removeFromBack();
34
System.out.printf(
"%n%d removed%n"
, removedItem);
35
list.print();
36
37
removedItem = list.removeFromBack();
38
System.out.printf(
"%n%d removed%n"
, removedItem);
39
list.print();
40
}
41
catch
(EmptyListException emptyListException)
42
{
43
emptyListException.printStackTrace();
44
}
45
}
46
}
// end class ListTest
The list is: -1
The list is: 0 -1
The list is: 0 -1 1
The list is: 0 -1 1 5
Fig. 21.5
|
ListTest
class to demonstrate
List
capabilities. (Part 1 of 2.)