Java Reference
In-Depth Information
where identifier is a (reference) variable and the data type of (the object that) identifier
(points to) is the same as the data type of the objects that each vectorObject element points
to. Also, type is either a primitive type or the name of a class.
For example, suppose that you have the following statements:
Vector<String> stringList = new Vector<String>();
//Line 1
stringList.addElement("One");
//Line 2
stringList.addElement("Two");
//Line 3
stringList.addElement("Three");
//Line 4
System.out.println("stringList: " + stringList);
//Line 5
for (String str : stringList) //Line 6
System.out.println(str.toUpperCase()); //Line 7
The statement in Line 1 creates the Vector object stringList to create a list of String
objects. The statements in Lines 2 through 4 add the string objects with the values "One" ,
"Two" , and "Three" , respectively, to stringList . The statement in Line 5 outputs the
values of the string objects of stringList . Note that the output of the statement in
Line 5 is:
stringList: [One, Two, Three]
The foreach loop in Lines 6 and 7 processes each element of stringList one at a time
and outputs each string in uppercase letters. More specifically, the output is:
ONE
TWO
THREE
The program StringVectorExampleII.java , which shows how to use a foreach loop
to process string Vector lists, can be found with the Additional Student Files at
www.cengagebrain.com. The program IntVectorExampleII.java shows how a foreach
loop, using the auto-unboxing feature of primitive data types, can be used to process the
elements of a Vector object of int values.
9
QUICK REVIEW
1 . An array is a structured data type with a fixed number of elements. Every
element is of the same type, and the elements are accessed using their
relative positions in the array.
2 . Elements of a one-dimensional array are arranged in the form of a list.
3 . An array index can be any expression that evaluates to a nonnegative integer.
The value of the index must always be less than the size of the array.
4 .
In Java, an array index starts with 0 .
5 .
In Java, [] is an operator, called the array subscripting operator.
 
 
Search WWH ::




Custom Search