Question

How would I search for the employee "Tim"?

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

Solution

Option 1 : Loop through the ArrayList to find the employee

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

Option 2 : Store the employees in some fast searchable data structure like a 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"));

No searching is very simple

  Employee emp = employeeMap.get("Tim");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top