简体   繁体   English

如何将值设置为存储在 ArrayList 中的特定属性?

[英]How can I set values to specific attributes stored inside of an ArrayList?

I have a lot of objects stored inside of an ArrayList called allAnimals.我在名为 allAnimals 的 ArrayList 中存储了很多对象。 Each object has 4 attributes for example Animal(02,"tiger",270.0,16) //ID,name,weight,age每个 object 有 4 个属性 例如 Animal(02,"tiger",270.0,16) //ID,name,weight,age

I have the user choose from 5 different options with a switch statement.我让用户使用 switch 语句从 5 个不同的选项中进行选择。 Four cases for each one and the fifth that lets the user change every single attribute.每种情况有四种情况,第五种情况允许用户更改每个属性。

Inside of each case I hade a Scanner to get his input(for example name) and after that I tried to use the allAnimals.set(i, name);在每个案例中,我都有一个扫描仪来获取他的输入(例如姓名),然后我尝试使用 allAnimals.set(i, name); //i for index //i 为索引

And I got an error.我得到了一个错误。 (incompatible types: java.lang.String cannot be converted to Animal) (不兼容类型:java.lang.String 无法转换为 Animal)

I was thinking storing each attribute into variables exept the one that is going to change and then remove the animal from the list and then add it back with the new attribute.我正在考虑将每个属性存储到变量中,除了将要更改的变量,然后从列表中删除动物,然后将其与新属性一起添加回来。

For example if user wanted to change the name.例如,如果用户想更改名称。 Store ID,weight,age into variables allAnimals.remove(i) allAnimals.add(new Animal(02, user_input, 270.0, 16);将 ID、体重、年龄存储到变量 allAnimals.remove(i) allAnimals.add(new Animal(02, user_input, 270.0, 16);

But I don't think that this is efficient.但我不认为这是有效的。

I also thought of adding some Setter methods in the Animal class but I don't know how it will work.(or if it will work at all)我还想过在 Animal class 中添加一些 Setter 方法,但我不知道它是如何工作的。(或者它是否会工作)

I'm new to this so sorry if I didn't explain something.如果我没有解释什么,我对此很抱歉。

Here is some of my code:这是我的一些代码:

System.out.println("Choose what you want to change from the menu below.");
System.out.println("--------------------------------------------------");
System.out.println("1. Change only its ID.");
System.out.println("2. Change only its name.");
System.out.println("3. Change only its weight.");
System.out.println("4. Change only its age.");
System.out.println("5. Change everything.");

Scanner get_new_option = new Scanner(System.in);
int option = get_new_option.nextInt();

switch(option)
{
   case 1:
   //Haven't tried to change the ID cause I have to test that the given ID doesn't already exist within the ArrayList

     System.out.println("Enter its ID.");
     Scanner get_new_id = new Scanner(System.in);
     int code = get_new_id.nextInt();
   break;

  case 2:
     System.out.println("Enter its name.");
     Scanner get_new_name = new Scanner(System.in);
     String name = get_new_name.nextLine();
     AllAnimals.set(i,name); //error here


  break;  
 //case 3:
 //break;
 }

Also here is a method that I use to initialise 15 animals that already shoudl exist in the zoo.这里还有一种方法,我用它来初始化动物园中已经存在的 15 只动物。 I just call the method in my main.我只是在我的 main 中调用该方法。

static ArrayList<Animal> AllAnimals = new ArrayList<Animal>();
static void InitAnimals()
{
    AllAnimals.add(new Animal(1,"turtle",90,150));
    AllAnimals.add(new Animal(2,"dolphine",80,17));
    AllAnimals.add(new Animal(3,"crocodile",75,20));
    AllAnimals.add(new Animal(4,"cheetah",120,20));
    AllAnimals.add(new Animal (5,"tiger",200,20));
    AllAnimals.add(new Animal(6,"owl",40,20));
    AllAnimals.add(new Animal(7," bonobo",80,25));
    AllAnimals.add(new Animal(8,"brown bear",450,24));
    AllAnimals.add(new Animal(9,"anacoda",60,18));
    AllAnimals.add(new Animal(10,"bald eagle",63,30));
    AllAnimals.add(new Animal(12,"panda",200,24));
    AllAnimals.add(new Animal(13,"jaguar",260,21));
    AllAnimals.add(new Animal(14,"orangutan",430,26));
    AllAnimals.add(new Animal(15,"shark",300,26));
}

No need to rewrite objects, just get the object you need by id and change the fields in it directly or, for example, through setters无需重写对象,只需通过 id 获取您需要的 object 并直接更改其中的字段,或者例如通过 setter

Given below is how you can change the name of the animal at index, i :下面给出了如何在索引i处更改动物的名称:

System.out.print("Enter new name: ");
String name = scanner.nextLine();
Animal animal = allAnimals.get(i);
animal.setName(name);
allAnimals.set(i, animal);

where scanner is an object of Scanner and setName is public setter method for the attribute, name .其中scannerScanner的 object , setName是属性namepublic设置方法。

Note: You should not create a new Scanner object for each input.注意:您不应为每个输入创建新的Scanner object。 Remove all Scanner instantiations and create just one instance eg Scanner scanner = new Scanner(System.in);删除所有Scanner实例并仅创建一个实例,例如Scanner scanner = new Scanner(System.in); at the beginning of the method where you have created the menu.在您创建菜单的方法的开头。 Then, you can reuse scanner for all inputs inside the method.然后,您可以对方法内的所有输入重用scanner

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

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