使用用户输入值显示数组列表中的特定对象

时间:2015-06-22 18:01:44

标签: java arraylist

我正在尝试创建一个程序,用户输入动物相关值,然后创建动物对象,然后将其保存到数组列表中。我遇到问题的地区如下所示。填充数组列表后,我无法弄清楚如何使用用户输入(选择)在数组列表中查找值索引。 (int index = animalList.indexOf(?))

非常感谢任何帮助

Scanner in = new Scanner(System.in);
  List <Animal> animalList = new ArrayList <Animal>();
    char ans;

do{  // User input
  Animal animal = new Animal(); // arraylist

   System.out.println("Animal's 'common' name: ");
     animal.setName(in.next());

   System.out.println("Animal's class: ");
     animal.setAnmlClass(in.next());

   System.out.println("Vertabrate or Invertabrate: ");
     animal.setCharVert(in.next());

   System.out.println("Warm or Cold blooded: ");
     animal.setCharBld(in.next());

   System.out.println("Animal's habitat (general): ");
     animal.setCharHab(in.next());

   System.out.println("Would you like to enter in a new animal (y/n)? ");
     String answer = in.next();
        ans = answer.charAt(0);

    animalList.add(animal);

  }while(ans == 'y');

   System.out.println("Enter the animal you wish to view: ");
     String select = in.next();
     System.out.println(select);
     int index =  animalList.indexOf( ? );
     System.out.println(index);

5 个答案:

答案 0 :(得分:1)

您可以考虑使用动物名称作为键来制作HashTable而不是List。

答案 1 :(得分:0)

不幸的是,你需要一个带有while循环的自定义方法:

public int getIndexWithName(List<Animal> animals, String name){
    for (int index = 0; index < animals.size(); index++){
       if (name.equals(animals.get(index).getName())){
           return index;
       }
    }
    return -1;
}

并使用此:

int index =  getIndexWithName(animalList, select);
//Do some validation check
if (index < 0){
    //Give some error message
    System.out.println("That's not a common name I recognize!");
}

答案 2 :(得分:0)

 System.out.println("Enter the name of the animal you wish to view: ");
 String select = in.next();
 System.out.println(select); // Check that your toString() its proper
 //There's not a straight way to find each animal's index
// so you 'll have to write an int getIndex(select) method to do that and then you 'll call it
 int index = getIndext(select);
 System.out.println("The animal you chose has index: "+index);

答案 3 :(得分:0)

创建一个 HashMap<String, Animal> ,将动物的名称映射到该对象。

Scanner in = new Scanner(System.in);
List <Animal> animalList = new ArrayList <Animal>();

Map<String, Animal> map = new HashMap<>();

char ans;

do{  // User input
  Animal animal = new Animal(); // arraylist

 System.out.println("Animal's 'common' name: ");

 String name = in.next();
 animal.setName(name);
 map.put(name, animal);

System.out.println("Animal's class: ");
 animal.setAnmlClass(in.next());

System.out.println("Vertabrate or Invertabrate: ");
 animal.setCharVert(in.next());

System.out.println("Warm or Cold blooded: ");
 animal.setCharBld(in.next());

System.out.println("Animal's habitat (general): ");
 animal.setCharHab(in.next());

System.out.println("Would you like to enter in a new animal (y/n)? ");
 String answer = in.next();
    ans = answer.charAt(0);

animalList.add(animal);

}while(ans == 'y');

System.out.println("Enter the animal you wish to view: ");
 String select = in.next();
 System.out.println(map.get(select));

答案 4 :(得分:0)

在Roel&amp; amp;西奥多拉,我最终得到了这个。它可能不是最干净或最漂亮的&#34;这样做的方法,但它没有问题。 System.out.println(&#34;输入您要查看的动物:&#34;);          String select = in.next();

 for (int index = 0; index < animalList.size(); index++){

 if (select.equals(animalList.get(index).getName())){
     System.out.println(index);

     System.out.format("%n"); // aesthetic break
     System.out.print(animalList.get(index).getName()+": ");
     System.out.print(animalList.get(index).getAnmlClass()+", ");
     System.out.print(animalList.get(index).getCharVert()+", ");
     System.out.print(animalList.get(index).getCharBld()+" Blooded, ");
     System.out.print(animalList.get(index).getCharHab()+" Terrain");
     System.out.format("%n"); // aesthetic break
   }
 else{
     System.out.println("The Zoo does not house that animal currently");
 }
 }