从一个特定元素中找到所有的值。

huangapple go评论58阅读模式
英文:

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&lt;Animal&gt; an = new ArrayList&lt;Animal&gt;();
Animal a4 = new Animal();
a4.add(&quot;Tiffany&quot;, 10, &quot;Giraffe&quot;, &quot;Grass&quot;);
a4.add(&quot;Mayo&quot;, 30, &quot;Elephant&quot;, &quot;Water&quot;);
a4.add(&quot;Simba&quot;, 30, &quot;Turtle&quot;, &quot;Leaves&quot;);

答案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&lt;Animal&gt; an = new ArrayList&lt;Animal&gt;();
an.add(new Animal(&quot;Tiffany&quot;, 10, &quot;Giraffe&quot;, &quot;Grass&quot;));
an.add(new Animal(&quot;Mayo&quot;, 30, &quot;Elephant&quot;, &quot;Water&quot;));
an.add(new Animal(&quot;Simba&quot;, 30, &quot;Turtle&quot;, &quot;Leaves&quot;));

// String name = &quot;Mayo&quot;; // commented to get the user input with Scanner
Scanner scan = new Scanner(System.in);
System.out.println(&quot;Enter the animal&#39;s name, please...&quot;);
String name = scan.nextLine();

Optional&lt;Animal&gt; foundAnimal = an.stream().filter(animal -&gt; animal.getName().equals(name)).findFirst();

if (foundAnimal.isPresent()) { // if the animal is in the list
	System.out.println(&quot;Animals name: &quot; + foundAnimal.get().getName() + &quot;\n&quot; + &quot;Animals age: &quot;
			+ foundAnimal.get().getAge());
}

Also you can achieve this by using a simple foreach loop:

List&lt;Animal&gt; an = new ArrayList&lt;Animal&gt;();
an.add(new Animal(&quot;Tiffany&quot;, 10, &quot;Giraffe&quot;, &quot;Grass&quot;));
an.add(new Animal(&quot;Mayo&quot;, 30, &quot;Elephant&quot;, &quot;Water&quot;));
an.add(new Animal(&quot;Simba&quot;, 30, &quot;Turtle&quot;, &quot;Leaves&quot;));

//String name = &quot;Mayo&quot;; // commented to get the user input with Scanner
Scanner scan = new Scanner(System.in);
System.out.println(&quot;Enter the animal&#39;s name, please...&quot;);
String name = scan.nextLine();

for (Animal animal : an) {
	if (animal.getName().equals(name)) {
		System.out.println(&quot;Animals name: &quot; + animal.getName() + &quot;\n&quot; + &quot;Animals age: &quot; + 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&lt;String,Animal&gt; animalsMap=new HashMap&lt;String,Animal&gt;();
animalsMap.put(&quot;Tiffany&quot;,new Animal(&quot;Tiffany&quot;, 10, &quot;Giraffe&quot;, &quot;Grass&quot;));
animalsMap.put(&quot;Tiffany&quot;,new Animal(&quot;Mayo&quot;, 30, &quot;Elephant&quot;, &quot;Water&quot;));
animalsMap.put(&quot;Simba&quot;,new Animal(&quot;Simba&quot;, 30, &quot;Turtle&quot;, &quot;Leaves&quot;));

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&lt;String,List&lt;Animal&gt;&gt; animalsMap=new HashMap&lt;String,Animal&gt;();
List&lt;Animal&gt; animalList = new ArrayList&lt;Animal&gt;();
animalList.add(new Animal()
Animal a4 = new Animal(&quot;Tiffany&quot;, 10, &quot;Giraffe&quot;, &quot;Grass&quot;);
animalList.add(a4);
Animal a4 = new Animal(&quot;Tiffany&quot;, 11, &quot;boar&quot;, &quot;meat&quot;);
animalList.add(a4);
animalsMap.put(&quot;Tiffany&quot;,animalList );

List&lt;Animal&gt; animalList = new ArrayList&lt;Animal&gt;();
animalList.add(new Animal()
Animal a4 = new Animal(&quot;simba&quot;, 10, &quot;Giraffe&quot;, &quot;Grass&quot;);
animalList.add(a4);
Animal a4 = new Animal(&quot;simba&quot;, 11, &quot;boar&quot;, &quot;meat&quot;);
animalList.add(a4);
animalsMap.put(&quot;simba&quot;,animalList );

last of all if you insist on using a list:

Animal result;
list.stream().forEach((a)-&gt;{if (a.getName().equals(&quot;simba&quot;) {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 &quot;Animal [name=&quot; + name + &quot;, num=&quot; + num + &quot;, type=&quot; + type + &quot;, 
food=&quot; + food + &quot;]&quot;;
}
public static void main(String [] args) {
List&lt;Animal&gt; an = new ArrayList&lt;Animal&gt;();
// Add Address objects to the ArrayList
an.add(new Animal(&quot;Tiffany&quot;, 10, &quot;Giraffe&quot;, &quot;Grass&quot;));
an.add(new Animal(&quot;Mayo&quot;, 30, &quot;Elephant&quot;, &quot;Water&quot;));
an.add(new Animal(&quot;Simba&quot;, 30, &quot;Turtle&quot;, &quot;Leaves&quot;));
// Iterate over the ArrayList
for(Animal a:an){
if(a.getName() == &quot;Simba&quot;){
System.out.println(a.toString());
}
}
}
}

huangapple
  • 本文由 发表于 2020年7月30日 22:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63175134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定