我想将一些向量放入向量的向量中。我在一个循环中执行此操作,最后只有最后一个添加的向量,但与我要添加的向量的计数一样频繁。
public void initVectors() {
rows = new Vector<Vector<String>>();
Vector<String> data = new Vector<String>();
Vector<String> t = new Vector<String>();
String aLine;
try {
FileInputStream fin = new FileInputStream("module.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
// extract data
while ((aLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(aLine, ",");
t.clear();
while (st.hasMoreTokens()) {
t.addElement(st.nextToken());
// System.out.println(st.nextToken());
}
System.out.println(t);
System.out.println("add it");
rows.addElement(t);
}
Enumeration vEnum = rows.elements();
System.out.println("Elements in vector:");
while (vEnum.hasMoreElements()) {
System.out.print(vEnum.nextElement());
System.out.println();
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我的输出是:
[GDI 1, 4, 1.0]
添加它
[Physik, 6, 1.3]
添加它
向量中的元素:
[Physik, 6, 1.3]
[Physik, 6, 1.3]