![](/img/trans.png)
[英]Java String operations by only using charAt (lastIndexOf, concatenate, substring, contain)
[英]ArrayList - get “Contain” and “lastIndexOf” using String
我正在尝试使用String获取方法“contains”和“lastIndexOf”的正确输出,但它不会为我提供正确的输出,在这种情况下,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
}
}
您需要在Employee
(或任何其他类)中实现equals
和hashCode
,以便contains
, sort
或其他方法有效。
例如,此构思向导默认使用所有字段:
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
工作方式。
作为lastIndexOf的javadoc状态,如果找不到该对象,则得到-1
。 但是找不到它的原因是,你正在处理一个Employee
列表而不是一个String
列表。 您当然可以在Employee
-class上实现equals
和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);
您的employeeList
被声明为ArrayList<Employee>
,因此您的employeeList.lastIndexOf("Gus")
将无法工作,因为该列表包含Employee
对象,而"Gus"
是一个String
因此它在列表中没有任何位置。
关于“包含”,我无法在您的代码中找到对ArrayList.contains(*)
调用(前提是您正在讨论的方法)。 你能指出来吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.