您好,有什么方法可以将 vector 转换为数组列表吗?我想为我的表设置搜索选项,但看起来使用 arraylist 对我来说会更容易。我使用了搜索,但令人惊讶的是没有关于 vector 的信息。
public Vector<Vector<Object>> InfoForTheTable() {
Scanner s = null;
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
try {
s = new Scanner(new File("info.txt"));
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("")) {
String[] atoms = line.split("[#]");
Vector<Object> row = new Vector<Object>();
row.add(atoms[0]);
row.add(atoms[1]);
row.add(atoms[2]);
row.add(atoms[3]);
row.add(atoms[4]);
row.add(atoms[5]);
data.add(row);
}
}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
if (s != null) {
s.close();
}
}
return data;
}
最佳答案
而不是
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
我推荐使用:
List<List<Object>> data = new ArrayList<>();
或者更好:创建一个类 MyInfo 并将原子值分配给其属性 然后使用:
List<MyInfo> data = new ArrayList<>();
这是代码的更现代版本:
public List<Info> readInfoFromFile() {
List<Info> infoList= new ArrayList<>();
try (Scanner s = new Scanner(Paths.get("info.txt")){
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("")) {
String[] atoms = line.split("[#]");
Info info = new Info();
info.setA(atoms[0]);
info.setB(atoms[1]);
info.setC(atoms[2]);
info.setD(atoms[3]);
info.setE(atoms[4]);
info.setG(atoms[5]);
infoList.add(rowinfo);
}
} catch(IOException e) {
e.printStackTrace();
}
return data;
}
根据需要替换属性及其类型。
关于java - 将 vector 转换为数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200555/