繁体   English   中英

在Java中将变量元素添加到数组中

[英]Adding a variable element to an array in 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]);
   }

这将定期打印出阵列,并且此部分有效。

    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获取输入。 它将用户输入存储到变量中。

但是,我尝试的方法不起作用。 相反,它只是说“此令牌后应有VarialDeclaratorId”,并突出显示我创建的新数组。

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

数组的大小恒定,您无法调整大小。 您可能想要的是一个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;

更好,更“ Java”的方法是使用List ,因为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()末尾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)); 
}

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


使用Map也是一个好主意。

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

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

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

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

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

然后要添加新元素,只需使用上方的put函数将其添加到末尾,传入从用户inout获得的元素:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM