Java ArrayList-获取“;包含;及;lastIndexOf“;使用字符串

Java ArrayList-获取“;包含;及;lastIndexOf“;使用字符串,java,arraylist,Java,Arraylist,我试图使用String为“contain”和“lastIndexOf”方法获得正确的输出,但它不会为那些方法提供正确的输出,在这种情况下,“contain”为false或true,以及“lastIndexOf”元素的位置。请问我怎么做?非常感谢 public class Employee { public static final int size = 0; String firstName; String surname; int yearOfBirth;

我试图使用String为“contain”和“lastIndexOf”方法获得正确的输出,但它不会为那些方法提供正确的输出,在这种情况下,“contain”为false或true,以及“lastIndexOf”元素的位置。请问我怎么做?非常感谢

public class Employee {
    public static final int size = 0;
    String firstName;
    String surname;
    int yearOfBirth;
    String PPSNumber;
    String email;
    String phoneNumber;

    public Employee(String firstName, String surname, int yearOfBirth, String PPSNumber, String email, String phoneNumber) {
        this.firstName = firstName;
        this.surname = surname;
        this.yearOfBirth = yearOfBirth;
        this.PPSNumber = PPSNumber;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }
}
//____________________________________________________//

import java.util.ArrayList;

public class EmployeeManagement {
    public static void main(String[] args) {
        ArrayList<Employee> employeeList = new ArrayList<>();

        employeeList.add(new Employee("Charlie", "Charles", 1991, "234567b", "charlie@b.ie", "7654321"));
        employeeList.add(new Employee("David", "Davies", 1992, "5213452d", "david@d.ie", "352135613"));
        employeeList.add(new Employee("Levi", "Silva", 1990, "1234", "Levi@b.ie", "333333"));
        employeeList.add(new Employee("Gus", "Silva", 1993, "4321", "Gus@b.ie", "444444"));

        for (Employee getName : employeeList) {

            System.out.print(getName.firstName + ",         ");

        }

        System.out.println(" ");

        // contains(Employee)
        Employee Emp = new Employee("Gus", "Silva", 1993, "4321", "Gus@b.ie", "444444");
        System.out.println("Contains String: " + employeeList.contains(Emp));    // can't make give me the right contain answer


        // lastIndexOf()    
        Employee Charlie1 = new Employee("Charlie", "Charles", 1991, "234567b", "charlie@b.ie", "7654321");
        System.out.println("lastIndexOf:  " + employeeList.lastIndexOf(Charlie1));    // can't find the lastIndexOf


    }
}
import java.util.ArrayList;
公共类员工管理{
公共静态void main(字符串[]args){
ArrayList employeeList=新建ArrayList();
新增(新员工(“查理”、“查尔斯”,1991年,“234567b”)charlie@b.ie", "7654321"));
新增(新员工(“大卫”、“戴维斯”,1992年,“5213452d”)david@d.ie", "352135613"));
新增(新员工(“Levi”,“Silva”,1990年,“1234”,”Levi@b.ie", "333333"));
新增(新员工(“古斯”、“席尔瓦”,1993年,“4321”,”Gus@b.ie", "444444"));
for(员工getName:employeeList){
System.out.print(getName.firstName+“,”);
}
System.out.println(“”);
//包含(员工)
员工Emp=新员工(“古斯”,“席尔瓦”,1993,“4321”Gus@b.ie", "444444");
System.out.println(“包含字符串:”+employeeList.Contains(Emp));//无法使您的答案正确
//lastIndexOf()
雇员查理1=新雇员(“查理”,“查尔斯”,1991,“234567b”charlie@b.ie", "7654321");
System.out.println(“lastIndexOf:+employeeList.lastIndexOf(Charlie1));//找不到lastIndexOf
}
}

您需要在
Employee
中实现
equals
hashCode
(或任何其他类,以便
包含
排序
或其他方法工作

例如,此idea向导默认使用所有字段:

class Employee {
    public static final int size = 0;
    String firstName;
    String surname;
    int yearOfBirth;
    String PPSNumber;
    String email;
    String phoneNumber;

    public Employee(String firstName, String surname, int yearOfBirth, String PPSNumber, String email, String phoneNumber) {
        this.firstName = firstName;
        this.surname = surname;
        this.yearOfBirth = yearOfBirth;
        this.PPSNumber = PPSNumber;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (yearOfBirth != employee.yearOfBirth) return false;
        if (firstName != null ? !firstName.equals(employee.firstName) : employee.firstName != null) return false;
        if (surname != null ? !surname.equals(employee.surname) : employee.surname != null) return false;
        if (PPSNumber != null ? !PPSNumber.equals(employee.PPSNumber) : employee.PPSNumber != null) return false;
        if (email != null ? !email.equals(employee.email) : employee.email != null) return false;
        return phoneNumber != null ? phoneNumber.equals(employee.phoneNumber) : employee.phoneNumber == null;
    }

    @Override
    public int hashCode() {
        int result = firstName != null ? firstName.hashCode() : 0;
        result = 31 * result + (surname != null ? surname.hashCode() : 0);
        result = 31 * result + yearOfBirth;
        result = 31 * result + (PPSNumber != null ? PPSNumber.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0);
        return result;
    }
}

现在,只需添加所需的或必需的字段,您将看到
包含的
是如何工作的。

您的
员工列表
被声明为
数组列表
,因此您的
员工列表。lastIndexOf(“Gus”)
将无法工作,因为该列表包含
员工
对象,而
“Gus”
是一个
字符串,因此它不在列表中


关于“contain”,我在代码中找不到对
ArrayList.contains(*)
的调用(前提是您正在讨论的方法)。你能指出它吗?

如前所述,如果找不到对象,你会得到
-1
。但找不到它的原因是,你处理的是
员工的列表,而不是
字符串的列表。当然,你可以在
员工的
类上实现
等于
hashCode
,但是你的方式不够灵活。相反,你应该只过滤你的列表中你需要的元素,例如

// Get (or show) all employees, whose name is "some string" using Stream API
employeeList.stream()
            .filter(employee -> employee.firstName.contains("some string"))
            .collect(Collectors.toList()); // or: .forEach(System.out::println);
关于获取条目的最后一个索引……您真的需要这样的功能吗?如果需要,并且您没有要查询的实际对象(“Gus”的
Employee
对象)您不想实现
hashCode
equals
,我建议您只需在列表上迭代,并在第一次出现时
break

int lastIndex = -1;
for (int i = employeeList.size() - 1; i >= 0 ; i--) {
  Employee anEmployee = employeeList.get(i);
  if (anEmployee.firstName.equals("Gus")) {
    lastIndex = i;
    break;
 }
}
System.out.printf("lastIndexOf: %s%n", lastIndex);

我在代码中找不到对
ArrayList.contains(*)
的调用(前提是您正在讨论的方法)。你能指出它吗?刚刚更改了代码,你现在可以看到包含,我修复了对象的类型,但仍然不起作用。请参见我在回答中的注释。我已将代码更改为我真正需要的代码,并将对象的类型改为“Employee”,它以前是字符串,我在这里得到的代码工作,但应该不是我固定对象类型时的工作方式?谢谢。@Gab
包含
lastIndexOf
需要能够判断两个
雇员
实例是否相等。他们通过在
雇员
上调用
等于
来实现这一点。因此,作为公认的nswer说,你需要在你的
Employee
类中重写
equals
(和
hashCode
),并把你想要用来判断两个
员工是否相等的逻辑放在那里。然后
包含
lastIndexOf
都会像你所期望的那样工作。只是想知道你所说的“包含”到底是什么意思:它是否包含一名姓氏相同的员工?或相同的电子邮件?或在所有这些字段中具有相同的值?请将该逻辑放入
employee.equals
@Gab关于在重写
equals
时为什么应该始终重写
hashCode
,您可以看到。只要您的示例中的e代码是相关的,但该代码肯定是更大应用程序的一部分,因此这并不重要,如果您重写
equals
,则不重写
hashCode
将是一个坏主意。我想不使用hashCode和equals,只需更改代码来修复对象的类型,但仍然不起作用。如果您实现eq正确地使用equals和hashCode,那么您的代码应该可以工作。但是,您所做的似乎是筛选/搜索。我不会依赖equals/hashCode,而是根据您的筛选条件筛选列表。