Java Reference
In-Depth Information
Display 14.5
A Generic Ordered Pair Class
(part 1 of 2)
1
public class
Pair<T>
2 {
3
private
T first;
4
private
T second;
Constructor headings do not
include the type parameter in
angular brackets.
5
public
Pair()
6 {
7 first =
null
;
8 second =
null
;
9 }
10
public
Pair(T firstItem, T secondItem)
11 {
12 first = firstItem;
13 second = secondItem;
14 }
15
public void
setFirst(T newFirst)
16 {
17 first = newFirst;
18 }
19
public void
setSecond(T newSecond)
20 {
21 second = newSecond;
22 }
23
public
T getFirst()
24 {
25
return
first;
26 }
27
public
T getSecond()
28 {
29
return
second;
30 }
31
public
String toString()
32 {
33
return
( "first: " + first.toString() + "\n"
34 + "second: " + second.toString() );
35 }
36