【问题标题】:Adding a variable element to an array in java在java中将变量元素添加到数组中
【发布时间】:2016-05-15 14:29:24
【问题描述】:
  String plants [] = {"Sunflower", "Pea Shooter", "Cherry Bomb", "Wall-Nut", "Potato Mine", "Snow Pea", "Chomper    "};

  int sunlight [] = {50, 100, 150, 50, 25, 175, 150};

  for (int i = 0; i < plants.length; i++) {
   System.out.println ((i+1) + "\t" + plants [i] + "\t" + sunlight [i]);
   }

这会定期打印出数组,这部分工作。

    String addplant = IBIO.inputString ("\nWhich plant do you add? ");
    int addsl = IBIO.inputInt ("How much sunlight do you add to that plant? ");

    plants [] = {"Sunflower", "Pea Shooter", "Cherry Bomb", "Wall-Nut", "Potato Mine", "Snow Pea", "Chomper    ", addplant};

    sunlight [] = {50, 100, 150, 50, 25, 175, 150, addsl};

    for (int i = 0; i < plants.length; i++) {
    System.out.println ((i+1) + "\t" + plants [i] + "\t" + sunlight [i]); }

同样,我希望根据用户输入的值打印出一个新数组。新元素应出现在数组的末尾。

我必须使用 IBIO.input 获取输入。它将用户输入存储到一个变量中。

但是,我尝试的方法不起作用。相反,它只是说“在这个标记之后需要VariableDeclaratorId”并突出显示我创建的新数组。

我想知道如何将变量元素添加到我的数组中,但不确定如何执行此操作。我尝试用谷歌搜索很多东西,但似乎没有任何效果。

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    数组的大小是固定的,你不能调整它们的大小。可能你想要的是一个 ArrayList。您也没有问,但我注意到您有两个按索引关联的相关数据数组,这可能会变得混乱。由于植物可能具有两种以上的属性,我建议使用这样的类:

       private class Plant {
        String plant;
        int sunlight;
    
        public Plant(String plant, int sunlight) {
            this.plant = plant;
            this.sunlight = sunlight;
        }
    }
    
    
    public void someMethod() {
        ArrayList<Plant> plants = new ArrayList<Plant>();
        plants.add( new Plant("Sunflower", 50));
        plants.add( new Plant("Pea Shooter", 100));
        ...
    
    
        for (int i = 0; i < plants.size(); i++) {
            System.out.println((i + 1) + "\t" + plants.get(i).plant + "\t" + plants.get(i).sunlight);
        }
    }
    

    然后,如果您删除豌豆射手,您将不会忘记删除豌豆射手的阳光元素。另外,如果您决定这样做,那就太好了:

       private class Plant {
        String plant;
        int sunlight;
        float soilPh;
    
        public Plant(String plant, int sunlight, float soilPh) {
            this.plant = plant;
            this.sunlight = sunlight;
            this.soilPh = soilPh;
        }
    }
    
    
    public void someMethod() {
        ArrayList<Plant> plants = new ArrayList<Plant>();
        plants.add( new Plant("Sunflower", 50, 6.5f));
        plants.add( new Plant("Pea Shooter", 100, 7f));
        ...
    
    
        for (int i = 0; i < plants.size(); i++) {
            System.out.println((i + 1) + "\t" + plants.get(i).plant + "\t" + plants.get(i).sunlight + "\t" + plants.get(i).soilPh);
        }
    }
    

    需要导入的编辑:

    import java.util.ArrayList;
    

    【讨论】:

    • 谢谢,但 ArrayList 不起作用。我收到一堆错误说 InvalidAssignmentOperator。我必须导入什么?
    • 我编辑了我的答案以包含它。导入 java.util.ArrayList;
    【解决方案2】:

    更好更“Java”的方式是使用List,因为它具有更好的结构来操作其项目。

    ArrayList<String> plants = new ArrayList<>(Arrays.asList("Sunflower", "Pea Shooter", "Cherry Bomb", "Wall-Nut", "Potato Mine", "Snow Pea", "Chomper"));
    ArrayList<Integer> sunlight = new ArrayList<>(Arrays.asList(50, 100, 150, 50, 25, 175, 150));
    

    然后,您只需将一个项目添加到列表中,并在末尾使用 add() 方法。

    plants.add(addplant);
    sunlight.add(addsl);
    

    获取想要的项目与使用数组类似。使用get(index) 方法。以下方法假定您的列表大小相同。

    for (int i=0; i<plants.size(); i++) {
        System.out.println((i+1) + "\t" + plants.get(i) + "\t" + sunlight.get(i)); 
    }
    

    在另一种情况下,您可以将 for 循环中的条件更改为 Math.min(plants.size(), sunlight.size())


    Map 的用法也是一个好主意。

    Map<String, Integer> myMap = new HashMap<>();
    

    使用put(...)方法添加项目。

    【讨论】:

    • ArrayList 植物 = new ArrayList (“向日葵”、“豌豆射手”、“樱桃炸弹”、“Wall-Nut”、“土豆矿”、“雪豌豆”、“切碎器“); ArrayList sun = new ArrayList (50, 100, 150, 50, 25, 175, 150);这是我尝试过的,但我遇到了很多错误。 @Nikolas Charalambidis
    • 您必须使用Arrays.asList(...) 方法包装,以防只有一行中的初始化值。已编辑。
    【解决方案3】:

    无法添加数组。您可能知道这一点,因为您正在重新分配数组的值,将所有旧元素加上新元素放入其中。这会导致问题,因为您已经对其进行了硬编码,并且下次添加项目时它不会包含任何以前添加的新元素。

    最好的办法是使用地图,存储植物和日照量,如下所示:

    Map<String, Integer> plantsAndSunlight = new HashMap<String, Integer>();
    plantsAndSunlight.put("Sunflower", 50);
    plantsAndSunlight.put("Peashooter", 100);
    :
    :
    

    然后要添加一个新元素,只需使用上面的put 函数将其添加到末尾,传入您从用户输入输出获得的元素:

    String addplant = IBIO.inputString ("\nWhich plant do you add? ");
    int addsl = IBIO.inputInt ("How much sunlight do you add to that plant? ");
    
    plantsAndSunlight.put(addplant, addsl);
    

    【讨论】:

    • Map 没有add(..) 方法.. 它被称为put(..)
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多