Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

Java - pick random string from array of strings

0 contributions
8 points

1. Random string from array of strings

We need to generate random index of an array.
As nextInt function is called we pass int bound to have results within array size.

import java.util.concurrent.ThreadLocalRandom;

public class JavaRandomStringFromArray {
    public static String randomStringFromArr() {
        String[] arr = {"A", "B", "C", "D", "E", "F"};
        int randIdx = ThreadLocalRandom.current().nextInt(arr.length);
        String randomElem = arr[randIdx];
        return randomElem;

    public static void main(String[] args) {
        System.out.println(randomStringFromArr()); // F
        System.out.println(randomStringFromArr()); // B
        System.out.println(randomStringFromArr()); // A
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more