私は学校の問題に取り組んでいます。このメソッドの多くは既に実装されており、あまりにも多くの変更を加えることはできません。消費者外のリストを変更するには
実際には私はのスポットに変更することしかできません。
私が取り組んでいるメソッドのコードですが、いくつかの単語はオランダ語で書かれています。
ファイルの行を読み込み、テキストからアドレスを作成し(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メソッドを使用する必要があります。どんな助けもありがとうございます。あなたはJava7で作業しているとき
はリスト 'final'を作ってみます: 'final ArrayList adressen =新しいArrayList <>();'。 –
Berger
しかし、リストを変更できないようにしていませんか? – Typhaon
いいえ、後で他のオブジェクトを 'adressen'変数に割り当てることができないことを意味します。 – Berger