java - 加入两个列表并更改特定数据
问题描述
我有接下来的两个列表,我需要以这样的方式加入它,即每个人都拥有所有书籍并在加入期间更改数据:
注意: 它如何使工作超出范围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)
条件:
需要为“John Dou”制作所有书籍true
而对于其他人只有 3 本书使true
也有Marker
包含 Peson as Books 的类:
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() ))
但它只制作了 3 本书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
instream().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"
解决方案
考虑到当前的要求和班级设计,完成这项工作的唯一方法是让每个人都有自己的书籍副本。
基于当前代码
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));
}
}
推荐阅读
- javascript - Moment.Js calculate difference datetime and show result
- amazon-web-services - 为什么印度电话号码无法接收我通过 Amazon SNS 发送的 SMS 消息?
- bash - 如何将 bash 脚本中的多个输出行覆盖到终端中
- python-3.x - 准备 df 子集,仅保留在整个列列表中的列组(通过搜索过滤)中满足的那些行
- node.js - 无法使用 req.cookies 设置 cookie
- python - binance python api:出售任何资产的整个余额功能
- python - Pandas 中的条件百分位数排名
- c - 根据用户重复c程序
- javascript - 确定罗马数字转换器的键值对
- jupyter-notebook - 即使在 anaconda 中下载了模块,也没有名为“beautifulsoup4”的模块