简体   繁体   中英

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.

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?

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 =(

I am noobie I am trying to understand and to learn.

You should override Object.equals() on the Box class. Try to handle null correctly too. Because 2 Box with null names and/or null money are in fact equal.

(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).

Stop using raw types!

Box is generic, so if you are not targeting older Java versions, always add generic parameters !.

The declaration of find2 should be like this:

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 . equals will not work because you did not define an equals method in Box . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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