//contains(元素) //包含,返回一个boolean类型的值,如果集合中存在当前元素的话返回true,否则返回false
[Java] 纯文本查看 复制代码 public class EmployeeMessage{
String name ;
int number ;
double sallary ;
}
import java.util.Scanner;
import java.util.ArrayList;
public class Employee{
public static void main(String[] args){
ArrayList<EmployeeMessage> employee = new ArrayList<EmployeeMessage>();
addEmployee(employee);
}
[color=red]//查找某一名字是否在该集合中, 这个方法要怎么实现,为什么我用contains这个词写的返回值都是false?[/color]
public static void containsEmployee(ArrayList<EmployeeMessage> employee){
Scanner sc = new Scanner(System.in);
System.out.println("请输入所要查找的员工姓名");
String s = sc.next();
boolean b = employee.[color=red]contains[/color](s);
System.out.println(b);
}
public static void addEmployee(ArrayList<EmployeeMessage> employee){
EmployeeMessage em = new EmployeeMessage();
EmployeeMessage em1 = new EmployeeMessage();
EmployeeMessage em2 = new EmployeeMessage();
em.name = "张三";
em.number = 001;
em.sallary = 5000;
em1.name = "李四";
em1.number = 001;
em1.sallary = 6000;
em2.name = "王五";
em2.number = 003;
em2.sallary = 4000;
employee.add(em);
employee.add(em1);
employee.add(em2);
}
}
|