ArrayList - get"包含"和" lastIndexOf"使用String

时间:2017-02-22 11:54:49

标签: java arraylist

我试图为方法获得正确的输出"包含"和" lastIndexOf"使用String,但它不会给那些正确的输出,在这种情况下,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


    }
}

3 个答案:

答案 0 :(得分:2)

您需要在equals(或任何其他类)中实施hashCodeEmployee,以便containssort或其他方法有效。

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

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;
    }
}

现在,只需添加所需或必填字段,您就会看到contains的工作原理。

答案 1 :(得分:1)

the javadoc of lastIndexOf所述,如果找不到该对象,则会获得-1。但找不到它的原因是,您正在处理Employee列表,而不是String列表。您当然可以在equals课程上实施hashCodeEmployee,但这样您就不够灵活了。相反,您应该只过滤您需要的元素列表,例如

// 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);

关于获取条目的最后一个索引....你真的需要这样的功能吗?如果是,并且您没有要查询的实际对象(&#34; Gus&#34;)的Employee对象,并且您不想实现hashCode和{{1}我建议你在第一次出现时迭代列表和equals

break

答案 2 :(得分:0)

您的employeeList被声明为ArrayList<Employee>,因此您的employeeList.lastIndexOf("Gus")将无效,因为该列表包含Employee个对象,"Gus"为{{1}所以它在列表中没有任何地方。

关于“包含”,我在您的代码中找不到您对String的来电(前提是您正在谈论的方法)。你能指出来吗?