The following combination is based on the
combination shown in Segment 548, and reads the same sort of data
file, but differs in that the combination stores Movie
instances in
a vector, rather than an array. The highlighted expressions and statements
identify the principle differences:
import java.io.*; import java.util.*; public class Demonstrate { public static void main(String argv[]) throws IOException { Vector mainVector = Auxiliaries.readData("input.data"); int size = mainVector.size(); for (int counter = 0; counter < size; ++counter) { System.out.println( ((Movie) mainVector.elementAt(counter)).rating() ); } } }
import java.io.*; import java.util.*; public class Auxiliaries { public static Vector readData(String fileName) throws IOException { FileInputStream stream = new FileInputStream(fileName); InputStreamReader reader = new InputStreamReader(stream); StreamTokenizer tokens = new StreamTokenizer(reader); Vector v = new Vector(); while (tokens.nextToken() != tokens.TT_EOF) { int x = (int) tokens.nval; tokens.nextToken(); int y = (int) tokens.nval; tokens.nextToken(); int z = (int) tokens.nval; v.addElement(new Movie(x, y, z)); } stream.close(); return v; } }