简体   繁体   English

如何将ArrayLists添加到ArrayList?

[英]How do I add ArrayLists to an ArrayList?

So, I have already created an ArrayList<>, say Staff List. 所以,我已经创建了一个ArrayList <>,比如Staff List。

private List<Staff> staffs = new ArrayList<Staff>();

public StaffFacade() {
    staffs.add(new Staff("1", "James"));
    staffs.add(new Staff("2", "Mike"));
    staffs.add(new Staff("3", "Lina"));
    staffs.add(new Staff("4", "John"));
}

public List<Staff> getStaffs() {
    return staffs;
}

And I want to create another List that contains Staff List (by adding), so that I don't have to add the same Staff in StaffFacade? 我想创建另一个包含Staff List(通过添加)的List,这样我就不必在StaffFacade中添加相同的Staff了?

I already created this BorrowFacade : 我已经创建了这个BorrowFacade

private List<Borrow> borrows = new ArrayList<Borrow>();

public BorrowFacade() {
    borrows.add(new Borrow()) //How do I add this?
}

public List<Borrow> getBorrows() {
    return borrows;
}

Referring to my question above, I don't know how to add new Staff List that has already been created. 参考我上面的问题,我不知道如何添加已经创建的新员工列表。

This is the constructor for the Borrow List: 这是借阅列表的构造函数:

public Borrow(Date dueDate, Staff staff, Book books) {
    this.dueDate = dueDate;
    this.staff = staff;
    this.books = books;
}

Of course, I put Date there because I wanted to add Date inside the List too. 当然,我把Date放在那里因为我想在List里面添加Date。

MAJOR EDIT 主要编辑

Okay so let me try to put it this way. 好的,让我试着这样说吧。 I have 4 classes which is StaffFacade, BorrowFacade, Borrow and Staff . 我有4个班级,分别是StaffFacade, BorrowFacade, Borrow and Staff

This is what I wrote inside StaffFacade : 这是我在StaffFacade中写的内容

public class StaffFacade {
    private List<Staff> staffs = new ArrayList<Staff>();

    public StaffFacade() {
staffs.add(new Staff("1", "James"));
staffs.add(new Staff("2", "Mike"));
staffs.add(new Staff("3", "Lina"));
staffs.add(new Staff("4", "John"));
    }

    public List<Staff> getStaffs() {
        return staffs;
    }
}

BorrowFacade : BorrowFacade

public class BorrowFacade {
    private List<Borrow> borrows = new ArrayList<Borrow>();

    public BorrowFacade() {
        borrows.add(staffsList);
    }

    public List<Borrow> getBorrows() {
        return borrows;
    }
}

Borrow (parts of it, the rest are just setters and getters) (部分,其余只是制定者和吸气者)

public class Borrow {
    String id;
    Date dueDate;
    Staff staff;
    Book books;

    public Borrow(String id, Date dueDate, Staff staff, Book books) {
        this.id = id;
        this.dueDate = dueDate;
        this.staff = staff;
        this.books = books;
    }

Staff : 员工

public class Staff{
    String id, name;

    public Staff(String id, String name) {
        this.id = id;
        this.name = name;
    }

The problem is in BorrowFacade. 问题出在BorrowFacade上。 I don't know how to add List that has been created in StaffFacade into BorrowFacade's List which is List<Borrow> borrows; 我不知道如何将在StaffFacade中创建的List添加到BorrowFacade的列表中,该List<Borrow> borrows;List<Borrow> borrows;

I'm very sorry for the confusion. 我很抱歉这个混乱。 If anything please ask me. 如果有的话请问我。 I really want this program to work. 我真的希望这个程序能够运作。

If you have a collection you can addAll(someOtherCollection); 如果你有一个集合,你可以addAll(someOtherCollection); but I am not sure I fully understand your question: you refer to a 'constructor for the Borrow List' but you show a constructor for a Borrow class which is not a list. 但我不确定我是否完全理解你的问题:你引用了一个'Borrow List'的构造函数,但你显示了一个Borrow类的构造函数,它不是一个列表。

You seem to be mixing up an instance of an individual class (eg Book ) with a collection or plurality of that class: Book books (why is it plural? What are you trying to express?) 你似乎把一个单独的类的实例(例如Book )与一个集合或多个类混合: Book books (为什么它是复数?你想表达什么?)

Edit: 编辑:

Based on you comment, I think you're trying to understand how to construct the Borrow objects to be placed in the list. 基于您的评论,我认为您正在尝试了解如何构建要放置在列表中的Borrow对象。

The difference between constructed the Staff List is that you 'know' the staff ahead of time - albeit these are canned values. 构建员工名单之间的区别在于您提前“了解”员工 - 尽管这些是罐装价值。

The Borrow object seems to express a particular person borrowing a particular book due back on a certain date. Borrow对象似乎表达某个特定的人在特定日期借用某本书。 If so, you need to have these details somewhere, for example from a database. 如果是这样,您需要在某处(例如从数据库中)获取这些详细信息。 The reason you're having trouble is you're trying to construct these objects in your Facade instead of just encapsulating ones that already exist. 你遇到麻烦的原因是你试图在Facade中构造这些对象而不是仅仅封装已经存在的对象。

public class Facade {
    private List<Borrow> borrows = new ArrayList<Borrow>();

    // Pass the items in to the constructor
    public Facade(List<Borrow> borrows) {
        this.borrows.addAll(borrows);
    }

    // You could call this externally in a loop to populate one by one
    public void addBorrow(Borrow borrow) {
        borrows.add(borrow);
    }

}

To restate: your Staff and your Borrow objects have to come from somewhere, so if they're already in a collection, use addAll , if not, just iterate the list and call add . 重申:你的Staff和你的Borrow对象必须来自某个地方,所以如果它们已经在一个集合中,请使用addAll ,如果没有,只需迭代列表并调用add Don't construct the objects in your Facades. 不要在Facades中构造对象。

Edit 2: 编辑2:

In response to your amended question, you can't do this. 在回答您修正的问题时,您不能这样做。 You're trying to add a list of a particulr object (Staff) in to a list of another type of object (Borrow). 您正在尝试将特定对象(Staff)的列表添加到另一种类型的对象(Borrow)的列表中。 This is just inherently wrong. 这本质上是错误的。 I don't quite know how else to say it. 我不太清楚怎么说。 If you asked me to give you a List of my favourite Stack Overflow questions, would you expect to find my favourite Stack Overflow user in that list? 如果您让我给您一个我最喜欢的Stack Overflow问题列表,您是否希望在该列表中找到我最喜欢的Stack Overflow用户? This is the fundamental nature of type safety. 这是类型安全的基本特征。 Now, if you asked me to give you a list of my Favourite Things then it is perfectly reasonable to expect to find various types of things in there - Stack Overflow Questions, Wines, Foods, etc. because they would conceptually share a common Favourite parent class or interface. 现在,如果你让我给你一个我最喜欢的东西的清单,那么期望在那里找到各种类型的东西是完全合理的 - Stack Overflow问题,葡萄酒,食品等,因为它们在概念上会共享一个共同的Favourite父母类或接口。

To be frank, I think you neeed to (re-)read up on the basic nature of Java generics and type safety, but in pursuit of the almighty reputation, here goes: 坦率地说,我认为你需要(重新)阅读Java泛型和类型安全的基本性质,但为了追求全能的声誉,这里有:

Note: I'm using StaffMember and BorrowedItem as names here to try to illustrate the value of good naming conventions. 注意:我在这里使用StaffMemberBorrowedItem作为名称来试图说明良好命名约定的价值。

You seem to want a Facade class for reasons none of us understand. 您似乎想要一个Facade类,原因我们都不理解。 Okay, we can accept that. 好的,我们可以接受。 Your Facade class seems to contain a list of objects. 您的Facade类似乎包含对象列表。 You have created multiple classes to accomplish this, with no discernable difference between the two except which objects are listed inside. 您已经创建了多个类来完成此任务,除了列出哪些对象之外,两者之间没有明显的区别。 Generics to the rescue: 救援的仿制药:

public class Facade<T> {

    private List<T> list = new ArrayList<T>();

    public Facade(List<T> existingList) {
        list.addAll(existingList);
    }

}

This facade holds a list of objects, meaning you can do this: 这个外观包含一个对象列表,这意味着你可以这样做:

List<StaffMember> staffMembers= new ArrayList<StaffMember>();
// .. assume list is populated here
Facade<StaffMember> staffMembersFacade = new Facade<StaffMember>(staffMembers);

Likewise, with the same facade class: 同样,使用相同的外观类:

List<BorrowedItem> borrowedItems = new ArrayList<BorrowedItem>();
// ... populate borrowed items
Facade<BorrowedItem> borrowedItemsFacade = new Facade<BorrowedItem<(borrowedItems);

But you aren't adding StaffMember objects to the borrowedItemsFacade . 但是您没有将StaffMember对象添加到borrowedItemsFacade At least not directly - in your example a BorrowedItem has a Date and it also points to which StaffMember borrowed it. 至少不是直接 - 在你的例子中, BorrowedItem有一个Date ,它也指向StaffMember借用它。

So at this point you have two lists - a list of StaffMember s, and a list of BorrowedItem s but you really have to ask yourself what purpose does this serve? 所以此时你有两个列表 - 一个StaffMember列表和一个BorrowedItem列表但你真的要问自己这个服务的目的是什么? Doesn't it make more sense for a single StaffMember to have a List<BorrowedItem> to keep track of all the items they borrowed? 单个StaffMember是否更有意义使用List<BorrowedItem>来跟踪他们借用的所有项目?

class BorrowedItem {
    Date dueDate;
    StaffMember borrower;
}

class StaffMember {
    String name;
    List<BorrowedItem> borrowedItems;
}

Now this provides the opportunity to add a function to the StaffMember like this: 现在,这提供了向StaffMember添加功能的机会,如下所示:

List<BorrowedItem> getOverdueItems() {
    List<BorrowedItem> overdueItems = new ArrayList<BorrowedItem>();

    Date today = getTodaysDate(); // Calendar.getInstance etc.

    for (BorrowedItem borrowedItem : borrowedItems) {
        Date dueDate = borrowedItem.getDueDate();
        if (today.after(dueDate)) {
            overdueItems.add(borrowedItem);
        }
    }

    return overdueItems;
}

Do you see how you need to create meaningful relationships between these classes in order for there to be anything useful to happen? 您是否看到如何在这些类之间创建有意义的关系,以便有任何有用的事情发生?

Then you can add functions to let someone lend an item to another person, or take something from someone, etc. 然后,您可以添加功能,让某人将物品借给他人,或从某人那里拿走某些东西等。

So yeah, Collections.addAll is what you're looking for, I think. 所以,是的, Collections.addAll是你正在寻找的,我想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM