如何使用添加了toString的arraylist访问类中的成员

时间:2017-05-19 17:18:13

标签: java arraylist polymorphism

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

我有一个函数public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet)

问题: CargoShip有toString打印name,commissionYear等。 我想做两件事:首先,我想使用每个Ship的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;
}

如果您需要,可以使用CargoShip课程:

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

2 个答案:

答案 0 :(得分:2)

首先,您可以将列表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;
}

读数:

答案 1 :(得分:1)

你应该实现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 );
}
相关问题