0

I'm trying to rid of duplicates for my Bingo Program so I created an ArrayList and I want to remove the integer that is printed out in the loop. I think the loop I'm using is wrong but I'm not sure.

             List<Integer> list = new ArrayList<Integer>();
                for(int i = 1; i <= 75; i++){
                    list.add(i);
                }

                for (Integer s : list) {
                    Collections.shuffle(list);
                    System.out.println(s);
                    list.remove(//);
                }
3
  • 3
    Don't modify a list when you are iterating over it. Use an iterator. If you don't want duplicates, use a Set. Commented Oct 22, 2013 at 17:45
  • 2
    With the way you have constructed list, there won't be any duplicates to remove. Commented Oct 22, 2013 at 17:46
  • 1
    What you're trying to do doesn't make much sense. You would do better here to just say Collections.shuffle(list); for (Integer s : list) { System.out.println(s); } list.clear();. (And the last statement is only necessary if the block doesn't end right after.) Commented Oct 22, 2013 at 17:50

5 Answers 5

2

Do like this.

for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext();) {
      Integer s = iterator.next();
      System.out.println(s);
      iterator.remove();
 }
Sign up to request clarification or add additional context in comments.

6 Comments

Why would you use list.removeAll(Arrays.asList(s)); when you could just call list.clear()?
@DaoWen if i need to call list.clear() then i will do list = new ArrayList<integer>();. You know right list.clear()!= list.removeAll()
@ByteCode: Since s isn't an array, i assume you meant something else there. And the main difference between list.clear() and list.removeAll(Arrays.asList(some_array)) is that the latter creates an unnecessary list, and also requires creating an unnecessary array in order for it to even compile.
@cHao list.removeAll(Arrays.asList(s)); if he want remove duplicates
@ByteCode: Thing is, there are no duplicates. The list contains 75 unique ints from 1 to 75.
|
2

If you're removing all the integers, it would be a lot easier to just do this:

Collections.shuffle(list);
for (Integer s : list) {
    System.out.println(s);
}
list = new ArrayList<Integer>();

Or, if you really want to keep the same list instance:

Collections.shuffle(list);
for (Integer s : list) {
    System.out.println(s);
}
list.clear();

Just creating a new array list is more efficient because it allows the garbage collector to just collect the entire old list, rather than removing the entries one by one. If you have multiple references to the same instance, however, then you'd need to actually clear the list instead.

Also note that the shuffle call has been moved outside the loop. Once you've done one shuffle, the list is already randomized, so shuffling again is kind of pointless... If you really want a "better" shuffle, you could do something like call shuffle 7 times before the first loop.

4 Comments

What do you think what Collections.shuffledoes?
Just as a side note, even if shuffle does not force a ConcurrentModificationException (some implementations might circumvent this for RandomAccessLists) shuffling for each iteration again should be avoided for performance reasons. There’s no increase of randomness by shuffling multiple times.
This was really a test method. Should've included it in my question but I have a Call button which if clicked displays a random number and I want all 75 Call numbers to be different :l
@HassaanHafeez - So, your problem is actually completely different from the question you posted? How does that make sense? You should probably just post a new question.
1

I would suggest using a HashSet<Integer> which doesn't allow duplicates:

Set<Integer> set = new HashSet<Integer>();

Comments

1

Are you sure this is not what you need?

private static final int NUM_BALLS = 75;

List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i <= NUM_BALLS; i++){
    list.add(i);
}

Collections.shuffle(list);

while (!list.isEmpty()) {
    Integer s = list.remove(list.size() - 1); // for better performance as noted by @Holger
    System.out.println(s);
}

7 Comments

That works, but removing from the beginning of an ArrayList is not the best practice esp. when applied for all elements.
You are right, I thought it to be a novice question so I changed the code according to the OP's original logic for better understanding.
Yeah, but using list.remove(list.size()-1) would not change the semantic. Adding a note about why removing the list element would give a novice the chance to learn even more.
@Holger Wait. Now that I think about it, there is no difference between removing from the beginning or from the end of the list; internally, arraycopy() will have to copy all the elements to a new array after each removal nevertheless.
why should removing a single item cause the ArrayList to create a new array? Removing the last item does not copy anything.
|
1

use following code

    Random generate = new Random();
    Set<Integer> set = new HashSet<Integer>();
    for(int i = 1; i <= 75; i++){
       set.add(generate.nextInt(75));
    }
    Iterator<Integer> iterator = set.iterator();
    while(iterator.hasNext()){
        System.out.println(iterator.next());
        iterator.remove();
    }
    System.out.print(set.size());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.