A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马