在Java中将变量元素添加到数组中 [英] Adding a variable element to an array in java

查看:189
本文介绍了在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]);
   }

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

This prints out the array regularly, and this part works.

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

类似地,我希望根据用户输入的值来打印出一个新数组.新元素应出现在数组的末尾.

Similarly, I want a new array to be printed out based on the value the user inputs. The new element should appear at the end of the array.

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

I must get input using IBIO.input. It stores the user input into a variable.

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

However, what I tried does not work. Instead, it just says "VariableDeclaratorId expected after this token" and highlights the new array I made.

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

I am wondering how I would add a variable element to my array, and am unsure of how to do this. I tried googling many things but nothing seemed to have worked.

推荐答案

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

Arrays have a constant size, you can't resize them. Probably what you want is an ArrayList. Also you didn't ask but I noticed you have two arrays of related data correlated by index, this can get messy. Since plants are likely to have more than two kinds attributes I suggest using a class like this:

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

然后,如果删除豌豆射手",您将没有机会忘记删除豌豆射手"的阳光元素.另外,如果您决定执行以下操作,它也很干净:

Then if you delete Pea Shooters, you won't have the chance to forget deleting the sunlight element for Pea Shooters. Plus it's nice and clean if you decide to something like this:

   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中将变量元素添加到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆