在java中向数组添加变量元素

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

这会定期打印出arrays,这部分可以正常工作。

  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”并突出显示我制作的新数组。

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

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

  private class Plant { String plant; int sunlight; public Plant(String plant, int sunlight) { this.plant = plant; this.sunlight = sunlight; } } public void someMethod() { ArrayList plants = new ArrayList(); 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); } } 

然后,如果你删除Pea Shooters,你就没有机会忘记删除Pea Shooters的阳光元素。 如果你决定这样的话,它还是很干净的:

  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 plants = new ArrayList(); 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; 

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

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

然后,您只需使用方法add()将项添加到List中。

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

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

 for (int i=0; i 

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


使用Map也是一个好主意。

 Map myMap = new HashMap<>(); 

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

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

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

 Map plantsAndSunlight = new HashMap(); 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);