我有下两个列表,我需要以这样的方式加入它:每个人都拥有所有的Book并在加入过程中进行更改数据:
注意: 它如何使工作超出范围for() loop
?
List<Person> personList = new ArrayList<>()
personList.add(new Person("John", "Dou"))
personList.add(new Person("Ben", "Gun"))
personList.add(new Person("Andre", "McDonald"))
List<Books> booksList = new ArrayList<>()
booksList.add(new Book("Harry Potter part_1", true)
booksList.add(new Book("Harry Potter part_2", true)
booksList.add(new Book("Harry Potter part_3", true)
booksList.add(new Book("Harry Potter part_4", true)
booksList.add(new Book("Harry Potter part_5", false)
条件:
需要让“约翰·道恩”的所有书籍都是true
对于其他人,只有3本书使true
还具有Marker
类,该类包含与书籍一样的Peson:
new Marker( new Person, new Book, int )
这是我的代码,但不明白。它按原样更改:
List<Marker> joinList = new ArrayList<>()
for(int i = 0; i<personList.size(); i++) {
for(int k = 0; k<booksList.size(); k++) {
If(personList.get(i).getName().equals("John") {
booksList.get(k).setReading(true);
} else {
if(k < 3 && !personList.get(i).getName().equals("John")){
booksList.get(k).setReading(true);
} else {
booksList.get(k).setReading(false)
}
}
joinList.add(new Marker(personList.get(i), booksList.get(k), k))
}
}
它应该在范围for()
循环之外起作用
joinList.forEach( (x) ->
System.out.println(x.getPerson.getName() + " " +
x.getBook().getName()+" "+
x.getBook().getReading() ))
但是它只能制作三本书true
当前结果:
输出
John Dou : Harry Potter part_1", true
John Dou : Harry Potter part_2", true
John Dou : Harry Potter part_3", true
John Dou : Harry Potter part_4", false
John Dou : Harry Potter part_5", false
Ben Gun : "Harry Potter part_1", true
Ben Gun : "Harry Potter part_2", true
Ben Gun : "Harry Potter part_3", true
Ben Gun : "Harry Potter part_4", false
Ben Gun : "Harry Potter part_5", false
Andre McDonald : "Harry Potter part_1", true
Andre McDonald : "Harry Potter part_2", true
Andre McDonald : "Harry Potter part_3", true
Andre McDonald : "Harry Potter part_4", false
Andre McDonald : "Harry Potter part_5", false
====预期结果:====
输出
John Dou : Harry Potter part_1", true
John Dou : Harry Potter part_2", true
John Dou : Harry Potter part_3", true
John Dou : Harry Potter part_4", true
John Dou : Harry Potter part_5", true
Ben Gun : "Harry Potter part_1", true
Ben Gun : "Harry Potter part_2", true
Ben Gun : "Harry Potter part_3", true
Ben Gun : "Harry Potter part_4", false
Ben Gun : "Harry Potter part_5", false
Andre McDonald : "Harry Potter part_1", true
Andre McDonald : "Harry Potter part_2", true
Andre McDonald : "Harry Potter part_3", true
Andre McDonald : "Harry Potter part_4", false
Andre McDonald : "Harry Potter part_5", false
以后,我将在joinList
中使用stream().Collectors
以获得下一个结果:
John Dou : "Harry Potter part_1", "Harry Potter part_2", "Harry Potter part_3", "Harry Potter part_4", "Harry Potter part_5"
Ben Gun : "Harry Potter part_1", "Harry Potter part_2", "Harry Potter part_3"
Andre McDonald : "Harry Potter part_1", "Harry Potter part_2", "Harry Potter part_3"
答案 0 :(得分:1)
根据当前的要求和课堂设计,进行此项工作的唯一方法是让每个人都有自己的书籍副本。
基于当前代码
List<Marker> joinList = new ArrayList<>()
for(int i = 0; i<personList.size(); i++) {
Person person = personList.get(i);
for(int k = 0; k<booksList.size(); k++) {
Book book = booksList.get(k);
Book bookCopy = new Book();
bookCopy.setName(book.getName);
if(person.getName().equals("John" || k < 3) {
bookCopy.setReading(true);
} else {
bookCopy.setReading(false);
}
joinList.add(new Marker(person, bookCopy, k));
}
}