java - 在 Java 中编辑字符串 boolean 值和整数的 ArrayList

标签 java arraylist

我的编码课有一个作业,我已经完成了大部分,但我不知道如何完成最后一部分。

这是作业“为植物苗圃创建一个植物类,它具有与植物相关的五个属性:

Maximum Height in feet
Common name
Scientific name
Price
Whether or not it is fragile.

为 Plant 类创建您自己选择的两个方法。 允许用户从控制台创建 Plant 对象。创建植物对象后,将该对象添加到 Plants ArrayList 中。

允许用户编辑有关已输入的植物对象的任何信息。

...还有 10 个额外学分!!允许用户查看按价格(从最低到最高)、学名(按属的字母顺序排列)或通用名称(按第一个单词的第一个字母顺序排列)排序的植物。 作业是个人的。”

我的代码

    class nursery{

    private int height;
    private String cName;
    private String sName;
    private int cost;
    private boolean fragile;

    public nursery(int height, String cName, String sName, int cost, boolean fragile)
    {
        this.height=height;
        this.cName=cName;
        this.sName=sName;
        this.cost=cost;
        this.fragile=fragile;
    }
}

public class Nursery {




    public static void main(String[] args) {

        ArrayList<nursery> plant = new ArrayList<>();

        Scanner s = new Scanner(System.in);

        while(true){
            //get the plant varibles
            System.out.println("Enter the common name of the plant: ");
            String cName = s.next();
            System.out.println("Enter the scientific name of the plant: ");
            String sName = s.next();
            System.out.println("Enter the height of the plant: ");
            int height = s.nextInt();
            System.out.println("Enter whether the plant is fragile or not: ");
            boolean fragile =s.nextBoolean();
            System.out.println("Enter the price of the plant: ");
            int cost=s.nextInt();
            //add to the arraylist
            nursery Plant = new nursery(height, cName, sName, cost, fragile);
            plant.add(Plant);
            System.out.println("If u would like to stop entering press q.");
            String quit = s.next();
            //quit out if wanted
            if(quit.equals("q")||quit.equals("Q"))
            break;
        }



    }

}

我不知道该怎么做是“允许用户编辑有关已输入的植物对象的任何信息。”我尝试过搜索,但一直无法得到答案。

最佳答案

您已保留所有 nursery物体(行星)至ArrayList<nursery> plant ,所以您需要做的就是从列表中找到它并重置其值。

一个一般的例子可能是这样的:

nursery plant_to_update = null;
for (int i=0; i<plant.length; i++){
    current_plant = plant.get(i);
    // say user want to update planet with cName as 'planet 1'
    if(plan_to_update.cName == "planet 1"){ 
        plant_to_update = current_plant;
        break;
    }
}
if( plant_to_update != null){
    // update planet 1 with new value
    plant_to_update.setHeight(50);
    plant_to_update.setCost(60);
}

并且,在 nursery 中添加 setter 类以更新这些私有(private)成员

public void setHeight(int height){
    this.height = height;
}

public void setCost(int cost){
    this.cost = cost;
}

关于java - 在 Java 中编辑字符串 boolean 值和整数的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28239412/

相关文章:

java - 为什么我不能在 Java 中使用\u000D 和\u000A 作为 CR 和 LF?

java - 如何在Windows XP中更改JDK安装目录?

java - ArrayList 和 Collection 问题

java - 从java中的参数创建数组

java - 无法为接口(interface) android.app.IAlarmManager 调用无参数构造函数

java - 如果在try block 中使用PreparedStatement作为资源,会产生不同的结果

java - 闪屏图片不出现

java - java.io.* 中 * 的用途是什么

java - 如何将通用项添加到通用 ArrayList?

java - ArrayList 中存储的数组的逆序