简体   繁体   中英

find element in linked list java

I have a linked list full of employees for testing, I would like to iterate through it and see if an employee is found then return true, this does work slightly however it will only find the last employee in the list, what am I doing wrong? Also I have different types of employee such as manager who inherit from the account class, sorry if thats confusing! Sorry still a beginner!! Thanks

Here is my list with the accounts added:

LinkedList<Account> Accounts = new LinkedList<Account>(); 

Employee John = new Account("John", "password1");
Manager Bob = new Account("Bob", "password2");   

Here is the method which finds the employee/account.

private boolean check(String employee) {

                for (Account e : Accounts) {
               if (e.getEmployee().equals(employee)) {
                   return true;
               }
               }       
                return false;


    }

EDIT: Below is the code i have added, it works however only for the last user. I would like the method to return true if the account name is found in the linked list, so that the system can continue and ask for further info etc.., the name variable is located in my Account class

private boolean check(String employee){
     for(Account a : accounts){
        if(a.name.equals(employee)){
            return true;
        }
     }
     return false;
 }

无需迭代, list的contains方法将为您完成

if (e.getEmployee().equals(employee))

This will compare if the objects are the same, meaning they are the same in memory .

You need to specify what properties of an object have to equal in order to logically say that 2 different objects are the same when looking at one or more properties.

For example

John.equals(Bob) 

will never be true but if the object John and object Bob both have the name property a String = "Alex" then

John.getName().equalsIgnoreCase(Bob.getName())

will be true

There is no need to iterate over the list. So, you can replace your check method with

  private boolean check(String employee) {
           return Accounts.contains(employee);
}

You should further replace

 Employee John = new Accounts("John", "password1");
 Manager Bob = new Accounts("Bob", "password2");

with

 Employee John = new Account("John", "password1");
 Manager Bob = new Account("Bob", "password2");

You have two options here. The first is to change the signature of your check method to use an Account object, or extract a field from your Account object for comparison as such:

private boolean check(Account account){
      return accounts.contains(account);
}

However, this would require you to implement a custom equals() method for Account :

@Override
public boolean equals(Object obj){
    if(obj instanceof Account){
         Account acct = (Account)obj;
         //Compare any internal fields that mean it's "equal"
         if(acct.prop1 == this.prop1 && ...) {
            return true;
          }
    }

    return false;
}

Your other option is to just compare against a particular field in your iteration:

private boolean check(String emp){
     for(Account a : accounts){
        if(a.stringProperty.equals(emp)){
            return true;
        }
     }
     return false;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM