Java Reference
In-Depth Information
Display 6.5 Partially Filled Array Class (part 4 of 4)
117 public boolean empty()
118 {
119 return (numberUsed == 0);
120 }
121 public boolean full()
122 {
123 return (numberUsed == maxNumberElements);
124 }
125 public int getMaxCapacity()
126 {
127 return maxNumberElements;
128 }
129
130 public int getNumberOfElements()
131 {
132 return numberUsed;
133 }
134 }
TIP: Accessor Methods Need Not Simply Return Instance Variables
Note that in the class PartiallyFilledArray in Display 6.5, there is no accessor method
that returns a copy of the entire instance variable a . The reason that was not done is that,
when the class is used as intended, a user of the class PartiallyFilledArray would
have no need for the entire array a . That is an implementation detail. The other meth-
ods that start with get allow a programmer using the class to obtain all the data that he
or she needs.
The “for-each” Loop
As you have already seen, you can use a for loop to cycle through all the elements in
an array. For example:
double [] a = new double [10];
< Some code to fill the array a>
for ( int i = 0; i < a.length; i++)
System.out.println(a[i]);
The standard Java libraries contain definitions of a number of so-called collection
classes . A collection class is a class whose objects store a collection of values. You cannot
 
Search WWH ::




Custom Search