0

I am randomly selecting a string from an array, and after it is selected I would like to make it so that I cannot choose that string again. Should I "Delete" it or is there a better way?

Here is my code to randomly select it,

position = positionList[(int) (Math.random() * positionList.length)];

4 Answers 4

5

Deleting would be the better option if you use an ArrayList instead of an array. Because in case of array, you would have to either re-size it (that means, you have to create a new array everytime - Deadly idea. Avoid it), or set the index selected to null.

But the problem with this approach (Setting null) is that, you may have to do random selection many times to get one valid random index especially when you are left with only 1 valid index. So, it's better to use a List, and delete the selected element from it.

List<String> list = new ArrayList<String>();  

// Populate your list.

Collections.shuffle(list);        // Shuffle the list.
String random = list.remove(0);   // Will remove the first element

Note that, if you want your original ArrayList, later one, then probably you can make a copy of it.

Sign up to request clarification or add additional context in comments.

Comments

3

The simplest way is

positionList[(int) (Math.random() * positionList.length)] = null;

but this seems to be closer to what you want

    String[] a = { "1", "2", "3", "4" };
    List<Integer> positions = new ArrayList<>(a.length);
    for (int i = 0; i < a.length; i++) {
        positions.add(i);
    }
    Collections.shuffle(positions);
    while(!positions.isEmpty()) {
        int i = positions.remove(positions.size() - 1);
        System.out.println(i);
    }

4 Comments

Would that also be able to store the selected string into position, then make it null?
This may be the simplest way, but is very inefficient. Consider the case where you are left with just a single non-null index. Then it may require the random number generator to run many times to get that index.
Guess what would be the probability to get an index say 5 from : (int) (Math.random() * positionList.length), given the array length is 100. This is the problem.
@Rohit Jain I agree, improved efficiency
0

I'd say you basically have three choices.

  1. Don't use an array. You could use a list instead which would let you easily remove the element
  2. Set the array element to null as suggested in the other answer
  3. Keep track of the array positions you've selected so far in some kind of data structure and check it each time you generate a random number.

Comments

0

First, you need to understand that arrays in Java have an immutable length. If you create an array with 5 elements / slots, it will always have 5 elements. You CANNOT change that.

So you literally cannot delete the i'th element of an array. What you CAN do is one of the following:

  • You can set the element at position i to null (assuming this is an array of objects).

  • You can create a new array that consists of the elements of the old array apart from the one you are "deleting". But note that you are not actually changing the original array.

There is a third option. Don't use an array at all. Use a List instead; e.g. an ArrayList or maybe a LinkedList depending on your application's requirements. The API allows you to both add elements to the list and remove elements from list ... and a lot more besides.

(The downside of using a ArrayList rather than an array is that the former takes marginally more space and is marginally slower. However it is unlikely to matter unless space usage and/or performance are critical concerns for your application.)

2 Comments

Would it be possible to store the selected string into position, then set that to null inside the array? If I wanted to go that path would I need to do a String compare for the entire array?
@Ryan. Yeah that may be possible, but don't do that. Why? See my comment on the first answer.

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.