简体   繁体   English

如何添加两个不同类的ArrayList,将一个抽象类扩展为一个ArrayList?

[英]How do I add two ArrayLists of different classes that extend an abstract class into one ArrayList?

So I have the two following arraylists: 所以我有以下两个数组列表:

private List<Manager> _managers = new ArrayList<Manager>(); //Manager extends User
private List<Employee> _employees = new ArrayList<Employee>(); //Employee extends User

I want to have an arraylist that combines both of these, storing all users, both employees and managers. 我想要一个将两者结合起来的数组列表,存储所有用户,包括员工和经理。

Currently I am trying the following solution: 目前,我正在尝试以下解决方案:

private List<User> _usrs = new ArrayList<User>();
/*
 *  function that I use before I get the current user list, preventing getting a outdated version of it
 */
public void refreshUserList() {
    this._usrs.clear();   //prevent duplicates 
    this._usrs.addAll(_employees); //add all employees to user list
    this._usrs.addAll(_managers);  //add all managers to user list
}

Is this a good way to solve this problem? 这是解决这个问题的好方法吗? I'm having some nullPointer iterator issues when iterating this list and before I get into solving this issue, I'd like to know if the problem isn't right here. 在迭代此列表时,我遇到了一些nullPointer迭代器问题,在开始解决此问题之前,我想知道问题是否就在这里。 Thanks. 谢谢。

Assuming that the Employee and the Manager classes both extend the User class, then yes, this is a good solution. 假设EmployeeManager类都扩展了User类,那么是的,这是一个很好的解决方案。 If you want to prevent duplicates, however, you can use a Set: 但是,如果要防止重复,可以使用Set:

private Set<User> _usrs = new HashSet<>();
/*
 *  function that I use before I get the current user set, preventing getting a outdated version of it
 */
public void refreshUserList() {
    this._usrs.addAll(_employees); //add all employees to user set
    this._usrs.addAll(_managers);  //add all managers to user set
}

You don't need to call clear(); 您无需调用clear(); , since Sets do not add duplicate elements. ,因为集合不会添加重复的元素。

This way, you will lose the special variables/methods that appear only for Managers or Employees, since all you will have will be the variables/methods available for Users. 这样,您将丢失仅对经理或雇员显示的特殊变量/方法,因为您所拥有的全部将是用户可用的变量/方法。 However, if you cast each User instance of your _usrs Set to Manager or Employee , then you can still get those special elements/methods: 但是,如果将_usrs Set的每个User实例_usrsManagerEmployee ,那么您仍然可以获得这些特殊元素/方法:

for (User user : _usrs) {
    if (user instanceof Manager) {
        Manager mng = (Manager) user;
        mng.manage(); // or whatever managers do that employees don't
    } else { //if no other classes extend the User class
        Employee emp = (Employee) user;
        emp.work(); // or whatever employees do that managers don't :P
    }
}

您可以使用Set而不是List来解决重复的问题,因为对于迭代器问题,您必须添加有关发生位置的更多信息。

I am guessing that your Manager and Employee classes are extending/ implementing your User class, Only then you will be able to combine the two arraylists. 我猜想您的ManagerEmployee类正在扩展/实现您的User类,只有这样您才可以组合两个数组列表。

If you want to prevent duplicates, use a Set , if you also want to maintain the order, use a LinkedHashSet . 如果要防止重复,请使用Set ;如果还要维护顺序,请使用LinkedHashSet

private LinkedHashSet<User> _usrs = new LinkedHashSet<User>();
public void refreshUserList() {
    usrs.clear();   //prevent duplicates 
    usrs.addAll(_employees); //add all employees to user list
    usrs.addAll(_managers);  //add all managers to user list
}

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

相关问题 如何将ArrayLists添加到ArrayList? - How do I add ArrayLists to an ArrayList? 当 ArrayList 和构造函数/getter 存储另外两个类时,如何从一个类添加到 ArrayList - How do I add to an ArrayList from one class, when the ArrayList and constructors/getters are stored 2 other classes 如何正确扩展此抽象类? - How do I properly extend this abstract class? 如何将不同的内部类添加到ArrayList? - How do I add different inner classes to an ArrayList? 一个类如何用两个不同的类扩展当一个类来自外部库而另一个是内置的时 - How a class can extend with two different classes While one class is from a external library and other is built in 如何将Arraylist分配给其他2个Arraylist? - How do I distribute an Arraylist to 2 other Arraylists? 如何将 Arraylist 拆分为多个 Arraylist? - How do I split Arraylist into several Arraylists? 如何添加两个元素数量不均匀的数组列表 - How do I add two arraylists with an uneven amount of elements 如何将两个数组列表组合为一个数组列表并将其传递给过程 - How to combine two arraylists into one arraylist and pass it to procedure 如何编写一个Hibernate查询,其中返回的列表是抽象类的不同子类? - How do I write a Hibernate query in which the returned list is different sub-classes of an abstract class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM