在java中向数组添加变量元素 String plants[]={“向日葵”、“豌豆射手”、“樱桃炸弹”、“壁挂坚果”、“土豆矿”、“雪豆”、“巧克力”}; int阳光[]={50,100,150,50,25,175,150}; 对于(int i=0;i

在java中向数组添加变量元素 String plants[]={“向日葵”、“豌豆射手”、“樱桃炸弹”、“壁挂坚果”、“土豆矿”、“雪豆”、“巧克力”}; int阳光[]={50,100,150,50,25,175,150}; 对于(int i=0;i,java,arrays,Java,Arrays,这会定期打印阵列,这部分工作正常 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.

这会定期打印阵列,这部分工作正常

  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(“\n您要添加哪个工厂?”);
int addsl=IBIO.inputInt(“你给那棵植物增加了多少阳光?”);
植物[]={“向日葵”、“豌豆射手”、“樱桃炸弹”、“墙坚果”、“土豆矿”、“雪豌豆”、“Chomper”、addplant};
阳光[]={50,100,150,50,25,175,150,addsl};
对于(int i=0;i
类似地,我希望根据用户输入的值打印出一个新数组。新元素应该出现在数组的末尾

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

然而,我所尝试的并不奏效。相反,它只是说“VariableDeclaratorId应该在这个标记之后”,并突出显示我创建的新数组

我想知道如何将变量元素添加到数组中,但不确定如何做到这一点。我试着用谷歌搜索很多东西,但似乎什么都没用

更好、更“Java”的方法是使用
List
,因为它具有更好的项目操作结构

    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]); }
获取想要的项目的工作原理与数组类似。使用
get(index)
方法。以下方法假设您的列表大小相同

plants.add(addplant);
sunlight.add(addsl);
使用方法
put(…)
添加项目更好、更“Java”的方法是使用
List
,因为它具有更好的项目操作结构

    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]); }
获取想要的项目的工作原理与数组类似。使用
get(index)
方法。以下方法假设您的列表大小相同

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

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

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

Map<String, Integer> myMap = new HashMap<>();
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);
   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);
    }
}

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

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

Map<String, Integer> myMap = new HashMap<>();
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);
   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);
    }
}

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

Map<String, Integer> myMap = new HashMap<>();
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);
   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);
    }
}

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

Map<String, Integer> myMap = new HashMap<>();
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);
   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);
    }
}

ArrayList plants=新ArrayList(“向日葵”、“豌豆射手”、“樱桃炸弹”、“壁挂坚果”、“土豆矿”、“雪豆”、“巧克力”);ArrayList阳光=新ArrayList(50、100、150、50、25、175、150);这是我尝试过的,但是我犯了很多错误@Nikolas CharalambidisYou必须使用
Arrays.asList(…)
方法包装,以防仅在一行中出现非专业化值。Edited.ArrayList plants=新ArrayList(“向日葵”、“豌豆射手”、“樱桃炸弹”、“壁挂坚果”、“土豆矿”、“雪豆”、“巧克力”);ArrayList阳光=新ArrayList(50、100、150、50、25、175、150);这是我尝试过的,但是我犯了很多错误@Nikolas CharalambidisYou必须使用
Arrays.asList(…)
方法包装,以防仅在一行中出现非专业化值。已编辑。谢谢,但ArrayList不起作用。我在说InvalidAssignmentOperator时遇到了很多错误。我必须导入什么?我编辑了答案以包含它。导入java.util.ArrayList;谢谢,但是ArrayList不起作用。我在说InvalidAssignmentOperator时遇到了很多错误。我必须导入什么?我编辑了答案以包含它。导入java.util.ArrayList<代码>映射
没有添加(…)方法。。它被称为
put(..)
<代码>映射没有添加(…)方法。。它被称为
put(..)