英文:
Finding all the values from one specific element
问题
我有一份动物列表和一些关于它们的信息,如果我想让用户能够通过输入动物的名称来查找该动物及其信息,我该怎么做?我已经将这些动物存储在一个列表中,就像这样:
List<Animal> animals = new ArrayList<Animal>();
Animal a1 = new Animal();
a1.add("Tiffany", 10, "Giraffe", "Grass");
a1.add("Mayo", 30, "Elephant", "Water");
a1.add("Simba", 30, "Turtle", "Leaves");
请注意,你的代码中似乎有一个小错误,应该为每个动物创建一个新的Animal对象,并将其添加到列表中。以上是已经修正的版本。
英文:
I have a list of animals and some information about them, what do I do if I want a user to be able to find for an animal and its information by typing the animal's name? I have stored the animals in a list like this:
List<Animal> an = new ArrayList<Animal>();
Animal a4 = new Animal();
a4.add("Tiffany", 10, "Giraffe", "Grass");
a4.add("Mayo", 30, "Elephant", "Water");
a4.add("Simba", 30, "Turtle", "Leaves");
答案1
得分: 1
我假设您有一个像这样的 Animal
类:
public class Animal {
String name;
int age;
String breed;
String eats;
// 获取器,设置器,带字段的构造函数
}
使用 Java 8,您可以进行如下操作:
List<Animal> an = new ArrayList<Animal>();
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));
// String name = "Mayo"; // 注释以使用 Scanner 获取用户输入
Scanner scan = new Scanner(System.in);
System.out.println("Enter the animal's name, please...");
String name = scan.nextLine();
Optional<Animal> foundAnimal = an.stream().filter(animal -> animal.getName().equals(name)).findFirst();
if (foundAnimal.isPresent()) { // 如果动物在列表中
System.out.println("Animals name: " + foundAnimal.get().getName() + "\n" + "Animals age: "
+ foundAnimal.get().getAge());
}
您也可以使用简单的 foreach
循环来实现相同的功能:
List<Animal> an = new ArrayList<Animal>();
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));
//String name = "Mayo"; // 注释以使用 Scanner 获取用户输入
Scanner scan = new Scanner(System.in);
System.out.println("Enter the animal's name, please...");
String name = scan.nextLine();
for (Animal animal : an) {
if (animal.getName().equals(name)) {
System.out.println("Animals name: " + animal.getName() + "\n" + "Animals age: " + animal.getAge());
}
}
输出:
Animals name: Mayo
Animals age: 30
英文:
I assume you've an Animal
class like this:
public class Animal {
String name;
int age;
String breed;
String eats;
// getters, setters, constructor with fields
}
with Java 8:
List<Animal> an = new ArrayList<Animal>();
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));
// String name = "Mayo"; // commented to get the user input with Scanner
Scanner scan = new Scanner(System.in);
System.out.println("Enter the animal's name, please...");
String name = scan.nextLine();
Optional<Animal> foundAnimal = an.stream().filter(animal -> animal.getName().equals(name)).findFirst();
if (foundAnimal.isPresent()) { // if the animal is in the list
System.out.println("Animals name: " + foundAnimal.get().getName() + "\n" + "Animals age: "
+ foundAnimal.get().getAge());
}
Also you can achieve this by using a simple foreach
loop:
List<Animal> an = new ArrayList<Animal>();
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));
//String name = "Mayo"; // commented to get the user input with Scanner
Scanner scan = new Scanner(System.in);
System.out.println("Enter the animal's name, please...");
String name = scan.nextLine();
for (Animal animal : an) {
if (animal.getName().equals(name)) {
System.out.println("Animals name: " + animal.getName() + "\n" + "Animals age: " + animal.getAge());
}
}
Output:
Animals name: Mayo
Animals age: 30
答案2
得分: 1
使用Map
Map<String, Animal> animalsMap = new HashMap<String, Animal>();
animalsMap.put("Tiffany", new Animal("Tiffany", 10, "Giraffe", "Grass"));
animalsMap.put("Tiffany", new Animal("Mayo", 30, "Elephant", "Water"));
animalsMap.put("Simba", new Animal("Simba", 30, "Turtle", "Leaves"));
要获取动物,请使用:
animalsMap.get("simba")
如果将列表与Map结合使用,可以处理多个名为"simba"的动物:
Map<String, List<Animal>> animalsMap = new HashMap<String, List<Animal>>();
List<Animal> animalList = new ArrayList<Animal>();
animalList.add(new Animal());
Animal a4 = new Animal("Tiffany", 10, "Giraffe", "Grass");
animalList.add(a4);
a4 = new Animal("Tiffany", 11, "boar", "meat");
animalList.add(a4);
animalsMap.put("Tiffany", animalList);
animalList = new ArrayList<Animal>();
animalList.add(new Animal());
a4 = new Animal("simba", 10, "Giraffe", "Grass");
animalList.add(a4);
a4 = new Animal("simba", 11, "boar", "meat");
animalList.add(a4);
animalsMap.put("simba", animalList);
最后,如果坚持使用列表:
Animal result;
list.stream().forEach((a) -> {
if (a.getName().equals("simba")) {
result = a;
}
});
英文:
Use Map
Map<String,Animal> animalsMap=new HashMap<String,Animal>();
animalsMap.put("Tiffany",new Animal("Tiffany", 10, "Giraffe", "Grass"));
animalsMap.put("Tiffany",new Animal("Mayo", 30, "Elephant", "Water"));
animalsMap.put("Simba",new Animal("Simba", 30, "Turtle", "Leaves"));
then to get an animal use:
animalsMap.get("simba")
this can be used with multiple animals called simba if you combine list with map:
Map<String,List<Animal>> animalsMap=new HashMap<String,Animal>();
List<Animal> animalList = new ArrayList<Animal>();
animalList.add(new Animal()
Animal a4 = new Animal("Tiffany", 10, "Giraffe", "Grass");
animalList.add(a4);
Animal a4 = new Animal("Tiffany", 11, "boar", "meat");
animalList.add(a4);
animalsMap.put("Tiffany",animalList );
List<Animal> animalList = new ArrayList<Animal>();
animalList.add(new Animal()
Animal a4 = new Animal("simba", 10, "Giraffe", "Grass");
animalList.add(a4);
Animal a4 = new Animal("simba", 11, "boar", "meat");
animalList.add(a4);
animalsMap.put("simba",animalList );
last of all if you insist on using a list:
Animal result;
list.stream().forEach((a)->{if (a.getName().equals("simba") {result=a});});
答案3
得分: 0
你的逻辑存在问题。你已经创建了一个ArrayList,它将存储Animal类的对象,所以你需要使用an.add(new Animal(参数/详细信息))
,然后在后面进行迭代。以下是参考代码 -
import java.util.ArrayList;
import java.util.List;
public class Animal {
private String name;
private int num;
private String type;
private String food;
public Animal(String name, int num, String type, String food) {
super();
this.name = name;
this.num = num;
this.type = type;
this.food = food;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Animal [name=" + name + ", num=" + num + ", type=" + type +
", food=" + food + "]";
}
public static void main(String [] args) {
List<Animal> an = new ArrayList<Animal>();
// 添加Animal对象到ArrayList
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));
// 遍历ArrayList
for(Animal a : an) {
if(a.getName().equals("Simba")) {
System.out.println(a.toString());
}
}
}
}
英文:
There are problems with your logic. You have creted an ArrayList which will store Animal class objects, so, u need to use an.add(new Animal ( parameters / details)) and later iterate over it. Use the following code for reference -
import java.util.ArrayList;
import java.util.List;
public class Animal {
private String name;
private int num;
private String type;
private String food;
public Animal(String name, int num, String type, String food) {
super();
this.name = name;
this.num = num;
this.type = type;
this.liveon = food;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Animal [name=" + name + ", num=" + num + ", type=" + type + ",
food=" + food + "]";
}
public static void main(String [] args) {
List<Animal> an = new ArrayList<Animal>();
// Add Address objects to the ArrayList
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));
// Iterate over the ArrayList
for(Animal a:an){
if(a.getName() == "Simba"){
System.out.println(a.toString());
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论