Java Reference
In-Depth Information
Display 15.9
A Sample Class for the Data in a Generic Linked List
1 public class Entry
2 {
3
private String item;
4
private int count;
5 public Entry(String itemData, int countData)
6 {
7 item = itemData;
8 count = countData;
9 }
10 public String toString( )
11 {
12
return (item + " " + count);
13 }
14 public boolean equals(Object otherObject)
15 {
16 if (otherObject == null )
17 return false ;
18 else if (getClass( ) != otherObject.getClass( ))
19 return false ;
20 else
21 {
22 Entry otherEntry = (Entry)otherObject;
23 return (item.equals(otherEntry.item)
24 && (count == otherEntry.count));
25 }
26 }
<There should be other constructors and methods, including accessor
and mutator methods, but we do not use them in this demonstration.>
27 }
Display 15.10
A Generic Linked List Demonstration (part 1 of 2)
1 public class GenericLinkedListDemo
2 {
3 public static void main(String[] args)
4 {
5 LinkedList3<Entry> list = new LinkedList3<Entry>( );
6 Entry entry1 = new Entry("Apples", 1);
7 list.addToStart(entry1);
8 Entry entry2 = new Entry("Bananas", 2);
9 list.addToStart(entry2);
10 Entry entry3 = new Entry("Cantaloupe", 3);
11 list.addToStart(entry3);
12 System.out.println("List has " + list.size( )
13 + " nodes.");
(continued)
Search WWH ::




Custom Search