
本文介绍了如何在 Java 中搜索 ArrayList 中的特定对象,重点在于理解 contains() 方法的局限性,并提供了一种基于循环的自定义搜索方案。通过示例代码,详细展示了如何根据对象的属性(例如产品名称)在 ArrayList 中查找目标对象,并提供相关的注意事项。
在 Java 中,ArrayList 是一种常用的动态数组,用于存储对象集合。当需要查找 ArrayList 中是否存在某个特定对象时,通常会想到使用 contains() 方法。然而,直接使用 contains() 方法来查找具有特定属性的对象(例如,根据产品名称查找 Product 对象)往往无法得到预期的结果。 这是因为 ArrayList 的 contains() 方法默认使用 equals() 方法来比较对象。 如果没有重写 equals() 方法,它将比较对象的引用,而不是对象的内容。
理解 contains() 方法的局限性
ArrayList 的 contains(Object o) 方法的工作原理是:遍历 ArrayList 中的每个元素,并使用 o.equals(element) 来比较给定的对象 o 与列表中的每个元素 element。 如果找到一个 element 使得 o.equals(element) 返回 true,则 contains() 方法返回 true;否则,返回 false。
在默认情况下,Object 类的 equals() 方法比较的是对象的引用。这意味着,只有当 o 和 element 指向内存中的同一个对象时,o.equals(element) 才会返回 true。
立即学习“Java免费学习笔记(深入)”;
在上面的问题描述的代码中,ArrayList 存储的是 Product 类的对象,而尝试使用 String 类型的 name 来调用 contains() 方法。 由于 String 对象和 Product 对象永远不会相等,因此 contains() 方法始终返回 false。
使用循环进行自定义搜索
为了根据对象的属性(例如产品名称)在 ArrayList 中查找目标对象,需要使用循环遍历 ArrayList,并手动比较每个对象的属性。
BJXShop网上购物系统是一个高效、稳定、安全的电子商店销售平台,经过近三年市场的考验,在中国网购系统中属领先水平;完善的订单管理、销售统计系统;网站模版可DIY、亦可导入导出;会员、商品种类和价格均实现无限等级;管理员权限可细分;整合了多种在线支付接口;强有力搜索引擎支持... 程序更新:此版本是伴江行官方商业版程序,已经终止销售,现于免费给大家使用。比其以前的免费版功能增加了:1,整合了论坛
以下是一个示例代码,展示了如何通过循环和属性比较来搜索 ArrayList 中的 Product 对象:
import java.util.ArrayList;
import java.util.Scanner;
class Product {
String name;
int price;
int id;
Product(int i, String name, int price) {
this.id = i;
this.name = name;
this.price = price;
}
}
public class Test {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(new Product(1, "Samsung", 10000));
al.add(new Product(2, "Apple", 20000));
al.add(new Product(3, "Nokia", 30000));
al.add(new Product(4, "Sony", 40000));
al.add(new Product(5, "LG", 50000));
System.out.println("Enter the name of the product to search:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
boolean found = false;
for (Product p : al) {
if (p.name.equals(name)) {
System.out.println("Product found: " + p.id + " " + p.name + " " + p.price);
found = true;
break; // 找到第一个匹配项后退出循环
}
}
if (!found) {
System.out.println("Product not found");
}
}
} 代码解释:
- 循环遍历: 使用 for-each 循环遍历 ArrayList 中的每个 Product 对象。
- 属性比较: 对于每个 Product 对象 p,使用 p.name.equals(name) 将其 name 属性与用户输入的 name 进行比较。
- 找到匹配项: 如果找到匹配的 Product 对象,则打印产品信息,并将 found 标志设置为 true,然后使用 break 语句退出循环。
- 未找到匹配项: 如果循环结束后 found 标志仍然为 false,则说明 ArrayList 中不存在具有指定名称的 Product 对象,打印 "Product not found"。
重写 equals() 方法 (可选)
另一种方法是重写 Product 类的 equals() 方法,使其根据产品名称进行比较。 如果重写了 equals() 方法,就可以直接使用 contains() 方法来查找 Product 对象。
class Product {
String name;
int price;
int id;
Product(int i, String name, int price) {
this.id = i;
this.name = name;
this.price = price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Product product = (Product) obj;
return name != null ? name.equals(product.name) : product.name == null;
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}注意事项:
- 如果重写了 equals() 方法,强烈建议同时重写 hashCode() 方法,以保证 equals() 方法和 hashCode() 方法的一致性。 这是因为在 HashMap 和 HashSet 等基于哈希表的集合中,hashCode() 方法用于确定对象的存储位置。 如果 equals() 方法相等但 hashCode() 方法不相等,则可能会导致对象无法正确地存储和检索。
修改后的 Test 类可以使用 contains 方法,但需要创建一个临时的 Product 对象用于比较:
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(new Product(1, "Samsung", 10000));
al.add(new Product(2, "Apple", 20000));
al.add(new Product(3, "Nokia", 30000));
al.add(new Product(4, "Sony", 40000));
al.add(new Product(5, "LG", 50000));
System.out.println("Enter the name of the product to search:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
Product searchProduct = new Product(0, name, 0); // 创建一个临时的Product对象
if (al.contains(searchProduct)) {
System.out.println("Product found");
} else {
System.out.println("Product not found");
}
}
} 总结
在 Java 中,使用 ArrayList 的 contains() 方法直接查找具有特定属性的对象通常无法得到预期的结果。 这是因为 contains() 方法默认使用 equals() 方法比较对象引用。 为了根据对象的属性进行搜索,可以使用循环遍历 ArrayList 并手动比较每个对象的属性。 此外,还可以通过重写 equals() 方法来实现自定义的对象比较逻辑,但需要注意同时重写 hashCode() 方法。根据实际情况选择最适合的搜索方案,以提高代码的可读性和效率。









