Java 使用用户输入值显示数组列表中的特定对象
我试图创建一个程序,用户输入与动物相关的值,然后创建动物对象,然后保存到数组列表中。我遇到问题的区域如下所示。填充数组列表后,我无法确定如何使用用户输入(select)在数组列表中查找该值索引。(int索引=动物学家索引(?) 非常感谢您的帮助Java 使用用户输入值显示数组列表中的特定对象,java,arraylist,Java,Arraylist,我试图创建一个程序,用户输入与动物相关的值,然后创建动物对象,然后保存到数组列表中。我遇到问题的区域如下所示。填充数组列表后,我无法确定如何使用用户输入(select)在数组列表中查找该值索引。(int索引=动物学家索引(?) 非常感谢您的帮助 Scanner in = new Scanner(System.in); List <Animal> animalList = new ArrayList <Animal>(); char ans; do{ //
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);
Scanner-in=新的扫描仪(System.in);
List animalList=newarraylist();
查尔安斯;
执行{//用户输入
动物=新动物();//数组列表
System.out.println(“动物的‘普通’名称:”);
animal.setName(in.next());
System.out.println(“动物类:”);
animal.setAnmlClass(in.next());
System.out.println(“Vertabrate或Invertabrate:”);
setCharVert(in.next());
System.out.println(“温血或冷血:”);
setCharBld(in.next());
System.out.println(“动物栖息地(一般):”;
setCharHab(in.next());
System.out.println(“您想输入新动物(y/n)?”;
字符串answer=in.next();
ans=答案。字符(0);
动物学家。添加(动物);
}而(ans='y');
System.out.println(“输入要查看的动物:”);
字符串select=in.next();
System.out.println(选择);
int index=animalist.indexOf(?);
系统输出打印项次(索引);
< /代码> 您可以考虑用一个动物名称作为关键字来创建一个哈希表,而不是一个列表。 不幸的是,您需要一个自定义方法,用于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;
}
public int getIndexWithName(列出动物、字符串名称){
对于(int index=0;index
并使用以下命令:
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!");
}
int index=getIndexWithName(animalList,select);
//做一些验证检查
如果(指数<0){
//给出一些错误信息
System.out.println(“这不是我认识的常见名称!”);
}
创建一个将动物名称映射到对象的HashMap
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));
Scanner-in=新的扫描仪(System.in);
List animalList=newarraylist();
Map Map=newhashmap();
查尔安斯;
执行{//用户输入
动物=新动物();//数组列表
System.out.println(“动物的‘普通’名称:”);
字符串名称=in.next();
动物名称(名称);
地图。放置(名称、动物);
System.out.println(“动物类:”);
animal.setAnmlClass(in.next());
System.out.println(“Vertabrate或Invertabrate:”);
setCharVert(in.next());
System.out.println(“温血或冷血:”);
setCharBld(in.next());
System.out.println(“动物栖息地(一般):”;
setCharHab(in.next());
System.out.println(“您想输入新动物(y/n)?”;
字符串answer=in.next();
ans=答案。字符(0);
动物学家。添加(动物);
}而(ans='y');
System.out.println(“输入要查看的动物:”);
字符串select=in.next();
System.out.println(map.get(select));
在Roel&Theodora的帮助下,我最终得到了这个。这可能不是最干净或“最漂亮”的方法,但它不会产生任何问题。
System.out.println(“输入要查看的动物:”);
字符串select=in.next()
for(int index=0;index
谢谢,唯一的问题是该项目的一个规范使用数组列表。如果不是必须的话,我会选择散列。使用HashMap
而不是HashTable
indexOf不会得到你想要的。本质上,用户没有办法输入一个字符串来满足Animal.equal()
。Animal
类是否基于name
重写了equals()
?谢谢,我用了一点你以我自己的方式建议的内容(有静态和非静态问题),无论如何。。。我是一个新手,所以它可能不漂亮,但它的工作。。。再次感谢
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));
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");
}
}