Ryan
Ryan

Reputation: 57

How to Remove string from array after randomly selecting it? Java

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)];

Upvotes: 0

Views: 4531

Answers (4)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

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);
    }

Upvotes: 3

Rohit Jain
Rohit Jain

Reputation: 213213

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.

Upvotes: 5

Stephen C
Stephen C

Reputation: 718738

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.)

Upvotes: 0

Memento Mori
Memento Mori

Reputation: 3402

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.

Upvotes: 0

Related Questions