Java Reference
In-Depth Information
Display 14.12
Using
UnorderedPair
(part 2 of 2)
11
{
12
System.out.println(p1.getFirst() + " and " +
13
p1.getSecond() + " is the same as");
14
System.out.println(p2.getFirst() + " and "
15
+ p2.getSecond());
16
}
17
}
18
}
Sample Dialogue
2
peanuts and beer is the same as
beer and peanuts
Self-Test Exercises
12. (This question refers to the starred section “Generic Methods.” You should skip
this question if you have not yet read that subsection.) Define a generic method
named
getMidindex
, which is like
getMidpoint
, but returns the index of the
array midpoint as opposed to the element at the midpoint.
13. (This question refers to the starred section “Inheritance with Generic Classes.”
You should skip this question if you have not yet read that subsection.) Is an
array of type
UnorderedPair<String>[]
also of type
Pair<String>[]
?
2
A note to the grammar police: I intentionally used “is” instead of “are.” If you read and understand
the text, you will realize that “
peanuts and beer
” is a single item. Starting the sentence with a low-
2
Chapter Summary
•
A
rrayList
is a parameterized class that is like an array that can grow (and shrink)
while the program is running.
•
An
ArrayList
has a number of methods that allow you to use it as a kind of
automated partially filled array.
•
You can cycle through all the elements in an
ArrayList
using a for-each loop.
•
You can define classes with one or more type parameters. Such classes are known
as
generic classes
.
2
A note to the grammar police: I intentionally used “is” instead of “are.” If you read and understand
the text, you will realize that “
peanuts and beer
” is a single item. Starting the sentence with a low-
ercase letter and the absence of a period are also intentional.