简体   繁体   English

在arrayList Java中搜索对象

[英]Search for an object in arrayList Java

package generics;

import java.util.ArrayList;
import java.util.List;


public class Generics {
    private static List <Box> newlist = new ArrayList<>();

    public static void main(String[] args) {

        newlist.add(new Box("charlie",30));
        newlist.add(new Box("max",29));
        newlist.add(new Box("john",22));

        // Testing method find -- Start
        find ("max",29);


        //Testing method find2 -- Start
        Box <String,Integer> search = new Box("max",29);

        find2(search);

    }

    public static void find (String parameter, Integer parameter1){

        for (Box e : newlist){
            if(e.getName() != null && e.getMoney() !=null 
                                   && e.getName().equals(parameter)
                                   && e.getMoney().equals(parameter1)){
                System.out.println("found on position " + newlist.indexOf(e));
                break;
            }

        }

    }

        public static void find2 (Box e){
        for (Box a : newlist){
            if (a.equals(e)){
                System.out.println("Found");
            }else {
                System.out.println("Not found");
            }
        }
    }
}


public class Box<T , D>{


    private T name;
    private D money;

    public Box(T name, D money) {
        this.name = name;
        this.money = money;
    }

    public T getName() {
        return name;
    }

    public D getMoney() {
        return money;
    }

    @Override
    public String toString() {
        return name + " " + money;
    }

}

Can someone show me how to search for an object in ArrayList. 有人可以告诉我如何在ArrayList中搜索对象。

Method find() it works perfect but in my opinion is wrong and the reason why I am thinking like that, because I am passing as parameter a string and an integer but should be an box object or maybe I wrong? 方法find()可以完美工作,但我认为这是错误的,也是我这么想的原因,因为我将字符串和整数作为参数传递但应该是box对象,还是我错了?

In my second method find2() I am trying to pass as parameter an object of Box and when I am trying to search for it I got a false result =( 在我的第二个方法find2()中,我试图将Box对象作为参数传递,当我尝试搜索它时,我得到了一个错误的结果=(

I am noobie I am trying to understand and to learn. 我是noobie,我试图理解和学习。

You should override Object.equals() on the Box class. 您应该在Box类上重写Object.equals()。 Try to handle null correctly too. 也尝试正确处理null。 Because 2 Box with null names and/or null money are in fact equal. 因为2个具有空名称和/或空货币的Box实际上是相等的。

(you DON'T need to override Object.hashCode() for this, but it's a good practice to do so, just in case it is used in a hashmap or hashset or such). (您不需要为此重写Object.hashCode(),但是这样做是一个好习惯,以防万一它在哈希映射或哈希集等中使用)。

Stop using raw types! 停止使用原始类型!

Box is generic, so if you are not targeting older Java versions, always add generic parameters !. Box是通用的,因此,如果您不针对较早的Java版本,请务必添加通用参数 !。

The declaration of find2 should be like this: find2的声明应如下所示:

public static void find2 (Box<String, Integer> e)

And you should check whether two boxes are equal in exactly the way you did in find . 并且您应该检查两个框是否完全符合find equals will not work because you did not define an equals method in Box . equals将不起作用,因为您没有在Box定义equals方法。 So: 所以:

for (Box<String, Integer> a : newlist){
    if (a.getName().equals(e.getName()) &&
        a.getMoney().equals(e.getMoney())){
        System.out.println("Found");
    }else {
        System.out.println("Not found");
    }
}

The easiest way to search and find something in an arraylist is to use the .equals method combined with a for loop to iterate through your lists. 搜索和查找数组列表中某些内容的最简单方法是将.equals方法与for循环结合使用以遍历列表。

for(int i = 0; i < newList; ++i)
{
if(newlist.equals(Stringname))
    {
     //it matches so do something in here
    }
}

what it is doing here is moving through the list 1 by 1 until it finds something that matches what you entered -> stringName 它在这里所做的是在列表中一一遍地移动,直到找到与您输入的内容相匹配的内容-> stringName

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

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