我正在为学校解决问题。很多这种方法已经实施,我不能做太多的改变。如何更改消费者之外的列表
其实我只能在具体点进行更改。
下面是我正在使用的方法的代码,虽然有些词语是荷兰语,但它应该是可读的。
它应该读取文件的行,从文本中创建地址(保存为(street +“”+ number +“”+ place))并将它们添加到返回的列表中。该文件以空行结束。
@Override
public List<Adres> query(ISpecification specification) {
if (specification instanceof FileSpecification) {
if (((FileSpecification) specification).toFileQuery().equals("ALL")) {
ArrayList<Adres> adressen = new ArrayList<>();
/*---start of my code*/
File studentF = new File(fsConnection.getStudentConnection());
try {
FileReader fr = new FileReader(studentF);
BufferedReader br = new BufferedReader(fr);
br.lines().forEach(new Consumer<String>(){
@Override
public void accept(String line) {
String[] words = line.split("\\s");
if(words.length == 3){
/*line i'm having trouble with*/adressen.add(new Adres(words[0], Integer.parseInt(words[1]), words[2]);
}
}
});
} catch (FileNotFoundException ex) {
Logger.getLogger(AdresFile.class.getName()).log(Level.SEVERE, null, ex);//Don't mind this
}
/*---end of my code*/
//System.out.println("query: Nog niet geimplementeerd!");
return adressen;
} else {
return null;
}
} else {
return null;
}
}
正如您所看到的,我想要访问消费者块之外的列表。我知道现在这是不可能的。我想创建一个不同的方法,但这是不允许的。我必须使用foreach方法。任何帮助表示赞赏。
尽量使列表'final' :'final ArrayList adressen = new ArrayList <>();'。 –
Berger
但是,这不会使列表不可变? – Typhaon
不,这只意味着您以后不能将其他对象分配给'adressen'变量。 – Berger