首页 > 解决方案 > 加入两个列表并更改特定数据

问题描述

我有接下来的两个列表,我需要以这样的方式加入它,即每个人都拥有所有书籍并在加入期间更改数据:

注意: 它如何使工作超出范围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

将来我将使用joinListinstream().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"

标签: javalistarraylist

解决方案


考虑到当前的要求和班级设计,完成这项工作的唯一方法是让每个人都有自己的书籍副本。

基于当前代码

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)); 
    }
}

推荐阅读