如何在Java中向对象列表添加元素。

huangapple go评论115阅读模式
英文:

How to add elements to an object list in java

问题

public class Bus{
    private int seats;
    private List<People> currentPeople;
    
    public Bus(int seats){
        this.seats = seats;
    }
    
    public void passengers(List<People> boarders, List<People> deboarders){
        this.currentPeople.remove(deboarders);
        this.currentPeople.add(boarders);
    }
}

我试图创建一个乘客列表在公共汽车上。我试图从列表中移除下车的乘客,并添加上车的乘客。但是Java不允许我将上车的乘客添加到currentPeople列表中。People是一个存储乘客姓名和票号的类。对此有任何帮助吗?我已经尝试解决了几个小时。


<details>
<summary>英文:</summary>

public class Bus{
private int seats;
private List<People> currentPeople;

public Bus(int seats){
  this.seats = seats;
}

public void passengers(List&lt;People&gt; boarders, List&lt;People&gt; deboarders){
this.currentPeople.remove(deboarders);
this.currentPeople.add(boarders);
}
I&#39;m trying to create a list of people on the bus. I&#39;m trying to remove deboarders(alighting passengers) from the list and add the boarders(boarding passengers). But Java won&#39;t let me add the boarders to the currentPeople list. People is a class which stores the name and ticket number of the passanger. Any help on this? I&#39;ve been trying to solve this for hours.    



</details>


# 答案1
**得分**: 3

添加一组对象到列表中,你需要使用 `addAll` 方法:

```java
this.currentPeople.addAll(boarders);

从列表中移除一组对象,你需要使用 removeAll 方法:

this.currentPeople.removeAll(deboarders);

注意,为了比较并从列表中移除正确的元素,removeAll 方法使用了 equals 方法。因此,你应该在你的 People 类中实现 equals 方法。

编辑:

People 类的 equals 方法:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    People people = (People) o;

    if (ticketNumber != people.ticketNumber) return false;
    return name != null ? name.equals(people.name) : people.name == null;
}

别忘了初始化 currentPeople 列表,否则会出现 NullPointerException

英文:

To add a collection of objects to a list you need to use addAll:

this.currentPeople.addAll(boarders);

To remove a collection of objects from a list you need to use removeAll:

this.currentPeople.removeAll(deboarders);

Be careful, in order to compare and remove the right elements from the list, removeAll uses equals method. So you should implement equals method in your People class.

EDIT:

equals method for People class:

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        People people = (People) o;

        if (ticketNumber != people.ticketNumber) return false;
        return name != null ? name.equals(people.name) : people.name == null;
    }

Don't forget to initialize currentPeople list, otherwise you will get NullPointerException.

答案2

得分: 0

将"add"更改为"addAll","remove"同样如此。正如Federico在评论中指出的那样,前提是"People"类已实现了equals方法。

英文:

Change add to addAll and likewise for remove.
As Federico points out in comments, assuming People has equals method implemented.

答案3

得分: 0

尝试使用 removeAll() 和 addAll() 方法,因为您正在将一个集合作为参数传递。

不要忘记在构造函数内部的行末添加“;”。

this.currentPeople.removeAll(deboarders);
this.currentPeople.addAll(boarders);

英文:

Try removeAll() and addAll() methods because you are passing a Collection as a parameter.

Do not forget " ; " at the end of the line inside your constructor.

this.currentPeople.removeAll(deboarders);
this.currentPeople.addAll(boarders);

huangapple
  • 本文由 发表于 2020年10月9日 23:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64283009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定