1

I am removing elements from an array in the below code. In this particular code I am removing the element at pos 2. How would I go about removing a random element in this array?

public class QuestionOneA2 {

public static void main(String[] args) {
    int size = 5;
    int pos = 2;

    String[] countries = {"Brazil", "France", "Germany", "Canada", "Italy", "England"};

        for (int i = 0; i < size; i++) {
            if(i == pos) {
                countries[i] = countries[size];
            }
            System.out.println(countries[i]);
        }   
    }
}
1
  • use e.g. int size = countries.length - 1 or just countries.length - 1 instead of a fixed size; otherwise you're doing it wrong. Commented Feb 7, 2016 at 13:53

3 Answers 3

1
Random r = new Random();
int result = r.nextInt(size);
//and select/remove countries[result]

This give you a pseudo-random number between 0 and 5 (exclusive). Be careful with your size variable, I think it's not well defined.

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

Comments

1

Remove this element:

int randomLocation = new Random().nextInt(countries.length);
// countries[randomLocation]  <--- this is the "random" element.

Or in 1 line:

countries[(new Random()).nextInt(countries.length)];

So in order to actually remove the element you can use ArrayUtils: First import these

import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

And then:

countries = ArrayUtils.removeElement(countries, countries[(new Random()).nextInt(countries.length)]);

If you really don't want to use ArrayUtils then you can use:

List<String> list = new ArrayList<String>(Arrays.asList(countries));
list.removeAll(Arrays.asList(countries[(new Random()).nextInt(countries.length)]));
countries = list.toArray(countries);

7 Comments

What do I put inside my for loop if I do this?
@StudentCoder I edited my answer with a clearer command to use (delete your for loop)
Thanks, I am actually trying to solve this question using a for loop though
There is no use for a for loop in this program it's a waste of space and time.
This is the first part of a 2 part question I am trying to do which will then ask the user to input the missing country and I was instructed to use a for loop, am I looking at his completely the wrong way?
|
0

In case you do not mind the order of the elements, you can also achieve this behaviour in constant time:

public <E> E removeRandom(List<E> list, Random random) {
    if (list.isEmpty())
        throw new IllegalArgumentException();

    int index = random.nextInt(list.size());
    int lastIndex = list.size() - 1;

    E element = list.get(index);
    list.set(index, list.get(lastIndex));
    list.remove(lastIndex);

    return element;
}

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.