Jesse Jesse - 6 days ago 5
Java Question

How to check the first index of an arraylist if it equals to a value?

Problem

How to check if a arraylist's first value is equal to some value?
In my attempt I am trying to search in "Status" to see if it contains the values.

List<Employees> data = new Arraylist<Employees>();
data.add(new Employees (Status, FirstName, LastName, age));


Attempt

if (data.contains("Inactive")) {
System.out.println("There are inactive Employees");
}
else if (data.contains("{Parttime")) {
System.out.println("There are part time Employees");
}
else {
System.out.println("All are full time Employees");
}


Currently I am receiving "All are full time Employees". I am not worried about the count. If there is one inactive employee then I should be notified.

I receive the output: [Lcom.foo.Employees;@28a418. Would this cause the fault?
System.out.println("data");

Answer

As I understand, you have a List<Employee> data; and Employee object contain string field - status. Try this:

String status = data.get(0).getStatus();
if (status != null && status.contains("Inactive")) {
    System.out.println("There are inactive Employees");
    } .....
Comments