搜索对象的数组列表

时间:2014-05-08 21:17:28

标签: java arraylist

我如何搜索员工“蒂姆”?

ArrayList <ClassName> employee = new ArrayList <ClassName>();
   employee.add(new ClassName("Name","Department",phoneNumber,"address")); 
   employee.add(new ClassName("Tim","something",9803845994,"something St"));

1 个答案:

答案 0 :(得分:3)

选项1:循环访问ArrayList以查找员工

  for(ClassName emp :  enployee)
  {
      if("Tim".equals(emp.getName()))
      {
           // do something
           break;
      }
  }

选项2:将员工存储在一些快速可搜索的数据结构中,例如HashMap

  Map<String, ClassName> employeeMap = new HashMap<>();
  employeeMap.add("Tim",  new ClassName("Tim","something",9803845994,"something St"));
  employeeMap.add("Jack",  new ClassName("Jack","something",22222222,"something St"));

没有搜索很简单

  Employee emp = employeeMap.get("Tim");