0
我正在將數組列表轉換爲java中的一個集合,並且生成的集合沒有產生預期的結果。數組設置不切斷Java數組
String[] characterName = {"C","A","P","T","A","I","N","A","M","E","R","I","C","A"};
String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
List<String> pool = new ArrayList<String>();
for(int i=0;i<characterName.length;i++) {
pool.add(characterName[i]);
}
int difference = 19-characterName.length;
for(int i=0;i<difference;i++) {
pool.add(alphabet[i]);
}
Collections.shuffle(pool);
Set<String> poolSet= new HashSet<>();
poolSet.addAll(pool);
我已經調試這一點,但我從水池中的addAll到HashSet的,它只會增加數組的前11個字符。即使當我調試時,它說池= 19,它只增加了11.我說錯了嗎?看起來很簡單,但它並沒有全部添加。任何輸入是不勝感激。
集合的定義不能有重複。當你添加一個重複的元素時,它會消失。 – hexafraction
「差異」變量的目的是什麼? –
這是完全意義上的。差異決定了我需要添加多少個字母才能添加到池中,以便達到19 –