代码之家  ›  专栏  ›  技术社区  ›  Mister M

在arrayList Java中搜索对象

  •  0
  • Mister M  · 技术社区  · 7 年前
    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;
        }
    
    }
    

    有人能告诉我如何在ArrayList中搜索对象吗。

    方法 查找() 它工作得很好,但在我看来是错误的 我之所以这样想,是因为我作为参数传递了一个字符串和一个整数,但应该是一个box对象,或者我错了?

    在我的第二种方法中 find2() 我试图将Box的一个对象作为参数传递,当我试图搜索它时,得到了一个错误的结果=(

    我是noobie,我正在努力理解和学习。

    3 回复  |  直到 7 年前
        1
  •  0
  •   Sweeper    7 年前

    停止使用原始类型!

    Box 是通用的,因此如果您不针对较旧的Java版本, 始终添加常规参数 !。

    声明 find2 应该是这样的:

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

    您应该检查两个框是否完全相同 find equals 将不起作用,因为您没有定义 等于 中的方法 .因此:

    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");
        }
    }
    
        2
  •  0
  •   user2023577    7 年前

    您应该重写对象。Box类上的等于()。 也请尝试正确处理null。因为名称为空和/或货币为空的两个盒子实际上是相等的。

    (您不需要为此重写Object.hashCode(),但这样做是一种很好的做法,以防在hashmap或hashset等中使用它)。

        3
  •  0
  •   Connor J    7 年前

    在arraylist中搜索和查找内容的最简单方法是使用 .equals 方法与for循环相结合,以遍历列表。

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

    它在这里所做的是逐1移动列表,直到找到与您输入的内容匹配的内容->字符串名称