I’m using arraylist in java and I need to add integers during 10 iterations (integer is got randomly from an array of integers named arrint) without any repetition:
for (int i =0; i<10; ++i)
array.add(integer);
and then add in the same array 20 other integers for the same array of integer(arrint) during 20 iteration without repetition
for (int i =0; i<10; ++i)
array.add(integer);
but repetition is permitted between the 10 first integers and the 20 integers.
thank you
Set, rather thanList, prevents duplicates. So you could establish aSet<Integer>and after populating it, add all of its elements to the List (withlist.addAll(set)). Then clear theSetand repeat for the next 20.It’s not clear from your description what you want to happen if duplicates are encountered. Do you want to add items into the
Setuntil it contains 10, just discarding duplicates? Or do you want to throw an exception if a duplicate is encountered?