如何使用 arraylist 访问类中的成员,该数组列表将其添加到字符串



我有一个类,比如说CargoShip,它是"星际争霸"的派生类,它实现了接口IStarcraft。

我有一个功能public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet)

问题:货船必须打印名称、佣金年份等的字符串。我想做两件事:首先,我想使用每艘船的toString(就像CargoShip中的那个(,其次我希望它按CommissionYear排序。

问题:在将 toString 添加到 arrayList 后,我不知道如何访问 commissionYear 字段。

ArrayList<String> strCommissions = new ArrayList<String>();
        for(ISpacecraft flee : fleet)
        {
            strCommissions.add(flee.toString());
        }
        //Collections.sort(//What to write here??//);
        return strCommissions;
}

如果您需要,这里是货船类:

package starfleet;
public class CargoShip extends Spacecraft{
    private int numberOfSpaceCranes;
    static int count = 0;
    public CargoShip(String name, int commissionYear, float maximalSpeed,int cargoCapacity, int numberOfSpaceCranes)
    {
        this.name = name;
        this.commissionYear = commissionYear;
        if(MaximalSpeed())
            this.maximalSpeed = maximalSpeed;
        this.cargoCapacity = cargoCapacity;
        this.numberOfSpaceCranes = numberOfSpaceCranes;
        count++;
    }
    public int getNumberOfSpaceCranes ()
    {
        return this.numberOfSpaceCranes;
    }

    @Override
    public String getName() {
        return this.name;
    }
    @Override
    public int getCommissionYear() {
        return this.commissionYear;
    }
    @Override
    public float getMaximalSpeed() {
        if(MaximalSpeed())
            return this.maximalSpeed;
        else
            return 0f;
    }
    @Override
    public int getCargoCapacity() {
        return this.cargoCapacity;
    }
    @Override
    public int getFirePower() {
        return this.firePower;
    }
    @Override
    public int getAnnualMaintenanceCost() {
        int cost = 0;
        this.commissionYear = 2000;
        cost += getCommissionYear();
        cost += (500 * this.numberOfSpaceCranes);
        cost += (2 * getCargoCapacity()); //To check: TotalCargoWeightCapacity?
        // TODO Auto-generated method stub
        return 0;
    }
    public String toString()
    {
        return
        "Name = " + getName() + System.lineSeparator() +
        "CommissionYear = " + getCommissionYear() + System.lineSeparator() +
        "MaximalSpeed = " + getMaximalSpeed() + System.lineSeparator() +
        "CargoCapacity = " + getCargoCapacity() + System.lineSeparator() +
        "FirePower = " + getFirePower() + System.lineSeparator() +
        "AnnualMaintenanceCost = " + getAnnualMaintenanceCost() + System.lineSeparator() + 
        "numberOfSpaceCranes = " + getNumberOfSpaceCranes() + System.lineSeparator();
    }
}

首先,您可以将列表fleet复制到新列表中:

ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);

然后,您可以按佣金年份对其进行排序:

Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));

然后创建字符串列表:

ArrayList<String> strCommissions = new ArrayList<String>();
objects.forEach(o -> strCommissions.add(o.toString()));

因此,您的函数变为:

public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet){
       ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
       Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));
       ArrayList<String> strCommissions = new ArrayList<String>();
       objects.forEach(o -> strCommissions.add(o.toString()));
       return strCommissions;
}

读数:

  • Java 8 Lambda:比较器示例

你应该实现 Comparable 并实现 compareTo 方法,或者如上所述可以使用 Comparator。

public int compareTo(CargoShip otherCargoShip) {
    int i = Name .compareTo(other.Name );
    if (i != 0) return i;
    i = CommissionYear.compareTo(other.CommissionYear);
    if (i != 0) return i;
    return Integer.compare(MaximalSpeed , other.MaximalSpeed );
}

最新更新