0

I have an Integer array and I want to randomly take an element from those who are not null. I have tried this but after the else, I cannot use return because the method is int. But I don't know what to write after else in order to return at the beginning and take a new random element.

public int getRandomPosition(){
    Random x = new Random();
    int b = x.nextInt(2*number0fCardPairs);
    if (myArray[b] != null){
       return b;
    }
    else{
        return;
    }
}
3
  • Is your array size is 2*numberOfCardPairs? Commented May 15, 2021 at 15:24
  • Could you please provide the code you have so far? I didn't got why you can't use return. Commented May 17, 2021 at 6:32
  • You need to return an int in your last return statement. Commented May 17, 2021 at 23:02

3 Answers 3

1

You could make your method recursive:

private Random x = new Random();

public int getRandomPosition(){
    int b = x.nextInt(sizeOfArray);
    if (myArray[b] != null){
       return b;
    }
    // else {
    return getRandomPosition();
}

or using a while-loop

private Random x = new Random();

public int getRandomPosition(){
    Integer result = null; 
    do {
      int index = x.nextInt(sizeOfArray);
      result = myArray[index];
    } while (result == null);
    return result.intValue();
}

Theoretically, however, the method could run indefinitely if you're really unlucky. Therefore, it probably makes sense to include a counter, so for example only try max. 20 times, then return null:

private Random x = new Random();

public int getRandomPosition(){
    Integer result = null; 
    int tries = 0;
    do {
      int index = x.nextInt(sizeOfArray);
      result = myArray[index];
    } while (result == null && ++tries < 20);
    return result.intValue();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. Note that there is a chance that the recursive method will result in StackOverFlowException since it might call itself many times depending on how many nulls are in the array and coincidence.
1

You can achieve this by using a loop.

public int getRandomPosition(){
    Random random = new Random();
    int randomIdx = random.nextInt(array.length);

    // while array at randomIdx is null, continue getting new randomIdx
    while (array[randomIdx] == null) {
        randomIdx = random.nextInt(array.length);
    }

    return randomIdx;
}

Note that if all the elements inside the array are null, it will be an infinite loop. Hope you find this helpful!

Comments

0

You can do:

Integer[] myArray = {1,2,3,null,5};

List<Integer> nonNullIntegers = Arrays.stream(myArray)
        .filter(Objects::nonNull)
        .collect(Collectors.toList());

Random random = new Random();
Integer randomInteger = nonNullIntegers.get(random.nextInt(nonNullIntegers.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.