2010-12-09 141 views
118

如何檢查在ArrayList中是否存在寫入掃描器的值?檢查ArrayList中是否存在值

List<CurrentAccount> lista = new ArrayList<CurrentAccount>(); 

CurrentAccount conta1 = new CurrentAccount("Alberto Carlos", 1052); 
CurrentAccount conta2 = new CurrentAccount("Pedro Fonseca", 30); 
CurrentAccount conta3 = new CurrentAccount("Ricardo Vitor", 1534); 
CurrentAccount conta4 = new CurrentAccount("João Lopes", 3135); 

lista.add(conta1); 
lista.add(conta2); 
lista.add(conta3); 
lista.add(conta4); 

Collections.sort(lista); 

System.out.printf("Bank Accounts:" + "%n"); 
Iterator<CurrentAccount> itr = lista.iterator(); 
while (itr.hasNext()) { 
    CurrentAccount element = itr.next(); 
    System.out.printf(element + " " + "%n"); 
} 
System.out.println(); 

回答

221

只需使用ArrayList.contains(desiredElement)。例如,如果你正在尋找從您的示例conta1帳戶,您可以使用類似:

if (lista.contains(conta1)) { 
    System.out.println("Account found"); 
} else { 
    System.out.println("Account not found"); 
} 

編輯: 注意,爲了這個工作,你需要正確地覆蓋equals()hashCode()方法。如果您使用的是Eclipse IDE,那麼你可以首先打開源文件爲您CurrentAccount對象和選擇Source > Generate hashCode() and equals()...

+7

equals()方法應該在CurrentAccount中被覆蓋以確定它們何時是同一個對象 – Javi 2010-12-09 23:28:16

+1

在這種情況下,hashcode()也需要被覆蓋。每hashcode()合約等於對象必須具有相同的散列碼。 – zockman 2010-12-09 23:46:40

32

更好,當你正在檢查一個值的存在,使用HashSetArrayList產生的這些方法。 Java文檔的HashSet說:"This class offers constant time performance for the basic operations (add, remove, contains and size)"

ArrayList.contains()可能需要遍歷整個列表,找到你要找的實例。

8

你好只是指我的回答this post在這裏解釋 有沒有必要迭代列表只是覆蓋等於方法。

修訂 equels方法而不是==

@Override 
public boolean equals (Object object) { 
    boolean result = false; 
    if (object == null || object.getClass() != getClass()) { 
     result = false; 
    } else { 
     EmployeeModel employee = (EmployeeModel) object; 
     if (this.name.equals(employee.getName()) && this.designation.equals(employee.getDesignation()) && this.age == employee.getAge()) { 
      result = true; 
     } 
    } 
    return result; 
} 

呼叫:

public static void main(String args[]) { 

    EmployeeModel first = new EmployeeModel("Sameer", "Developer", 25); 
    EmployeeModel second = new EmployeeModel("Jon", "Manager", 30); 
    EmployeeModel third = new EmployeeModel("Priyanka", "Tester", 24); 

    List<EmployeeModel> employeeList = new ArrayList<EmployeeModel>(); 
    employeeList.add(first); 
    employeeList.add(second); 
    employeeList.add(third); 

    EmployeeModel checkUserOne = new EmployeeModel("Sameer", "Developer", 25); 
    System.out.println("Check checkUserOne is in list or not "); 
    System.out.println("Is checkUserOne Preasent = ? "+employeeList.contains(checkUserOne)); 

    EmployeeModel checkUserTwo = new EmployeeModel("Tim", "Tester", 24); 
    System.out.println("Check checkUserTwo is in list or not "); 
    System.out.println("Is checkUserTwo Preasent = ? "+employeeList.contains(checkUserTwo)); 

} 
0

當數組列表包含原始數據類型的對象。

Use this function: 
arrayList.contains(value); 

if list contains that value then it will return true else false. 

當數組列表包含UserDefined DataType的對象時。

Follow this below Link 

How to compare Objects attributes in an ArrayList?

我希望這個解決方案將幫助你。 謝謝