簡體   English   中英

找出ArrayList是否包含一個以上元素

[英]Find out if an ArrayList contains more than one of an element

我有一個用戶名數組列表和一個密碼數組列表。 兩個或更多用戶可以使用相同的密碼。

ArrayList<String> u = new ArrayList<String>();
ArrayList<String> p = new ArrayList<String>();

這些數組是從以下格式的文件填充的:

username  password

像這樣

String line = null;
String split[] = null;
while ((line = reader.readLine()) != null){
   split = line.split("\\s+");
   u.add(split[0]);
   p.add(split[1]);
}

我有一個生成密碼的蠻力算法,我想查找生成的密碼是否匹配。

String password = bruteForce()
if (passwords.contains(password)){
    int index = passwords.indexOf(password);
    System.out.println("Username: " + usernames.get(index));
    System.out.println("Password: " + password);
}

如果沒有用戶使用相同的密碼,這將很好地工作。 如果兩個用戶具有相同的密碼; 它將僅捕獲第一個用戶並進行打印,而跳過第二個用戶。

我需要一種方法來確定是否多次出現相同的密碼,獲取每次出現的索引,然后獲取它們匹配的用戶名。

除了您不應該存儲純文本密碼或字符串密碼之外,還可以回答以下問題:

嘗試使用Collections.frequency

int amount = Collections.frequency(yourCollection, yourObject);

至於您的設計,您還可以嘗試一些約束:

List<Tuple<String, String>> accounts = new ArrayList<>();
//Or even making an account object and then
List<Account> accounts = new ArrayList<>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM