为什么当我在我的 ArrayList 上输入确切名称时 'contains' 方法 return 为假?

Why does the 'contains' method return false when I enter the exact name on my ArrayList?

我正在尝试制作一个可以添加客户姓名、年龄、联系电话和电子邮件的程序。而且我想搜索用户想要的名称,但即使我输入的名称完全相同,它也不会搜索名称。我该如何解决这个问题?

这是我的代码:

package com.company;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<customers> customers = new ArrayList<>();
        customers.add(new customers("Zen",19,"0912121212","zen@gmail.com"));
        customers.add(new customers("Mary",20,"09134343434","mary@gmail.com"));
        System.out.println("Enter name: ");
        String name = scan.nextLine();
        System.out.println(customers.contains(name));
    }
}
class customers{
    private String name;
    private int age;
    private String contactNumber;
    private String email;

    public customers(String name, int age, String contactNumber, String email) {
        this.name = name;
        this.age = age;
        this.contactNumber = contactNumber;
        this.email = email;
    }
}

name 是一个 String。您的 List 包含 customers 个实例,而不是 String 个。因此,您的 List 不包含 name.

为了通过另一种类型的键查找一种类型的实例,您可以使用 Map:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    Map<String,customers> customers = new HashMap<>();
    customers.put("Zen",new customers("Zen",19,"0912121212","zen@gmail.com"));
    customers.put("Mary",new customers("Mary",20,"09134343434","mary@gmail.com"));
    System.out.println("Enter name: ");
    String name = scan.nextLine();
    System.out.println(customers.containsKey(name));
}

或者,如果您想搜索具有特定名称的 customers 实例,您可以遍历 List 的元素(使用循环或使用 Stream).

例如:

System.out.println(customers.stream().anyMatch(c -> c.getName().equals(name)));

这是假设您的 customers class 有一个 getName() getter 方法。

将另一个构造函数添加到您的 class 中,仅用于命名并遍历数组列表中的所有对象以获得相同的名称。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<customers> customers = new ArrayList<>();
        customers.add(new customers("Zen",19,"0912121212","zen@gmail.com"));
        customers.add(new customers("Mary",20,"09134343434","mary@gmail.com"));
        System.out.println("Enter name: ");
        String name = scan.nextLine();
        customers obj = new customers(name);
        customers toBeChecked;
        for (int i=0; i<customers.size(); i++) {
            toBeChecked = customers.get(i);
            if(toBeChecked.getName().equals(obj.getName())) {
                System.out.println("Same name");
            }
        }
    }
}
class customers{
    private String name;
    private int age;
    private String contactNumber;
    private String email;

    public customers(String name, int age, String contactNumber, String email) {
        this.name = name;
        this.age = age;
        this.contactNumber = contactNumber;
        this.email = email;
    }
    public customers (String name) {
    this.name = name;
    }
    public String getName() {
        return name;
    }

}

List.contains()使用 Object.equals() 确定对象是否已在该列表中。

因此,一种方法可能是覆盖该方法:

public class Customer
{
  private String m_Name;
  private int m_Age;
  …

  @Override
  public final boolean equals( final Object o )
  {
    return o instanceof String name && name.equals( m_Name );
  }
}

虽然这可行,但不建议以这种方式实施 equals()(请参阅 here 作为起点)。

相反,您应该在列表中搜索名称:

String name = scan.nextLine();
System.out.println( customers.stream().anyMatch( c -> c.getName().equals( name ) ) );

一种完全不同的方法是不将 Customer 对象存储在 List 的实例中,而是存储在 Map 的实例中,名称作为键:

public class Main 
{
  public static void main( String... args ) 
  {
    Scanner scan = new Scanner(System.in);
    Map<String,Customer> customers = new HashMap<>();
    var customer = new Customer( "Zen", 19, "0912121212", "zen@gmail.com" );
    customers.put( customer.getName(), customer );
    customer = new Customer( "Mary", 20, "09134343434", "mary@gmail.com" );
    customers.put( customer.getName(), customer );
    System.out.println( "Enter name: " );
    String name = scan.nextLine();
    System.out.println( customers.containsKey( name ) );
  }
}

最后,如果您遵循 Java 语言的基本命名约定,通常会有帮助:class 名称以大写字母开头。

这里的问题是您没有用 String 类型定义 ArrayList。如果你想把客户留在你的名单中,你可以试试这个解决方案:

for(customers c:customers){
if(c.getName().equals(name)){
System.out.println(true);
}
 }

确保创建一个 getter ( getName() )。