Java Reference
In-Depth Information
5.5
Iterators
SR 5.24
a.
Scanner user =
new
Scanner (System.in);
b
.
Scanner infoFileScan =
new
Scanner (
new
File(
"
info.dat
"
));
c
. Scanner infoStringScan =
new
Scanner (infoString);
SR 5.25
The following code prints out the average number of characters per
line:
int
numChars = 0;
int
numLines = 0;
String holdLine;
// Read and process each line of the file
while
(fileScan.hasNext())
{
numLines++;
holdLine = fileScan.nextLine();
numChars += holdLine.length();
}
System.out.println ((
double
)numChars/numLines);
5.6
The
ArrayList
Class
SR 5.26
An
ArrayList
stores and manages multiple objects at one time. It allows
you to access the objects by a numeric index and keeps the indexes of
its objects continuous as they are added and removed. An
ArrayList
dynamically increases its capacity as needed.
SR 5.27
An
ArrayList
generally holds references to the
Object
class, which
means that it can hold any type of object at all (this is discussed
further in Chapter 8). A specific type of element can and should be
specified in the
ArrayList
declaration to restrict the type of objects
that can be added and eliminate the need to cast the type when
extracted.
SR 5.28
ArrayList<Die> dice =
new
ArrayList<Die>()
;
SR 5.29
[Andy, Don, Betty]
Search WWH ::
Custom Search