2016-11-21 140 views
0

對Java來說是全新的,我試圖從一個數組中找到匹配的元素到另一個數組中,似乎無法理解如何去做。下面是數據是如何和多遠我已經得到的樣品:在數組中找到匹配元素

在該代碼和打印該行後,這是數據是如何:

ArrayList<String> all_accounts = new ArrayList<String>(); 
all_accounts.add(acc); 
System.out.println("\nArray 1:" + all_accounts); 

結果數組1:

從Array 2
Array 1:[77737320] 
Array 1:[88405378] 
Array 1:[00056893] 
Array 1:[10709816] 

ArrayList<String> cancel_accounts = new ArrayList<String>(); 
cancel_accounts.add(cancel_acc); 
System.out.println("\nArray 2:" + cancel_accounts); 

結果:這裏

Array 2:[77737320] 
Array 2:[] 
Array 2:[] 
Array 2:[] 

堆棧,我仍然無法理解爲什麼它不匹配:

 String found = null; 
    for (String account: all_accounts) { 

     for (String canceled: cancel_accounts) { 
      System.out.println(canceled); 
      found = canceled; 
     } 
    System.out.println(found); 
    if(account.equals(found)) { 
       System.out.println(account); 
     } 

    } 

我需要在這種情況下找到匹配元素77737320。 感謝您的期待!

+0

可能重複[如何測試數組是否包含某個值?](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a -ertain-value) –

+0

@PeterPerháč問題是完全不同的,在這裏OP要求數組包含多於一個元素而不僅一個。 – user6904265

+0

@ user6904265我堅持認爲這個問題已經在這裏得到了肯定的回答,OP可以弄清楚如何看待我鏈接的其他問題。這是「做我的作業」類問題,不應該在這裏 –

回答

0

只需添加一個equals檢查你的 - 循環(甚至會工作,沒有List#contains法)

for(String account: all_accounts) { 
    System.out.println(account); 

    for(String canceled: cancel_accounts){ 
    System.out.println(canceled); 

    if(account.equals(cancelled)){ 
     //you've found first intersection, cancelled exists in both 
     System.out.println(canceled + " is the same as " + account); 

    } 
    } 
} 
+0

我想這樣,我仍然不明白爲什麼它不匹配: \t String found = null; \t爲(字符串帳戶:all_accounts){ \t \t 爲\t(字符串取消:cancel_accounts){ \t \t的System.out.println(取消); \t \t找到=取消; \t \t} \t \t System.out.println(found); \t \t if(account.equals(found)){System.out.println(account); \t} \t \t \t} – Andre

+0

EXACTLY嘗試,因爲上面的答案 - 和手錶,你把大括號。你在上面的評論中粘貼的是一個嚴重的邏輯錯誤。 – C0D3LIC1OU5

+0

我認爲問題在於所有數據都是一次一個元素,就像上面的數據樣本一樣,這就是爲什麼我找不到匹配。 – Andre

0

你可以通過一個列表循環和搜索第二列表中的第一個每個元素。

for (String account: all_accounts) { 
    if (cancel_accounts.contains(account) { 
     // Match found - do something.... 
     System.out.println(account); 
    } 
} 
+0

只需注意,如果值是'primitive'類型,則'List#contains'方法將不起作用。儘管應該使用'String'。 – C0D3LIC1OU5

1

你可以套之間實現這個作爲交集:

Set<String> set_all_account = new HashSet<String>(all_accounts); 
Set<String> set_cancel_accounts = new HashSet<String>(cancel_accounts); 
set_all_account.retainAll(set_cancel_accounts); 
set_all_account.forEach(x -> System.out.println("Element matched: "+x)); 

或由在他的評論kartiks說,你可以調用retainAll方法是直接all_accounts陣列上:

all_accounts.retainAll(cancel_accounts); 
all_accounts.forEach(x -> System.out.println("matched element: "+x)); 

請注意此解決方案,因爲在這種情況下retainAll直接應用於ArrayList並對其進行修改(因爲您可以看到最終結果是在all_accounts陣列中)。而且重複的元素保留在結果數組中。

末實現(如果你想計算路口和打印結果都在同一行,也該版本不斷重複的元素):

all_accounts.stream().filter(x -> cancel_accounts.contains(x)).forEach(x -> System.out.println("matched element: "+x)); 
3

+1回答user6904265

但是,你需要不創建新的HashSet。你可以使用ArrayList.retainAll()。如果您想維護all_accounts列表,請創建一個新的克隆並使用它。

+0

我用你的提示更新了我的答案。但是如果你不想在最終數組中使用重複值(如果輸入數組包含任何值),你應該使用'Set'。 ;) – user6904265

相關問題