1

I have an ArrayList which is defined outside the main method, just inside the class StringRandomize:

 public static ArrayList<String> countries = new ArrayList<String>();

I also initialized a random object.

 Random obj = new Random();

Then I add some Strings to the list:

    StringRandomize.countries.add("USA");
    StringRandomize.countries.add("GB");
    StringRandomize.countries.add("Germany");
    StringRandomize.countries.add("Austria");
    StringRandomize.countries.add("Romania");
    StringRandomize.countries.add("Moldova");
    StringRandomize.countries.add("Ukraine");

How do I make those strings appear randomly? I need output like "Germany", "Moldova" and so on.
I need exactly the strings in the output, not their IDs. Thanks for your help.

3
  • countries.get(index) Commented Feb 12, 2016 at 22:11
  • "Cannot resolve symbol 'index' " What am I doing wrong? Commented Feb 12, 2016 at 22:13
  • 1
    If you want to print the all content (and not just fetch a random element), you can also consider shuffling the list with Collections.shuffle. Commented Feb 12, 2016 at 22:16

3 Answers 3

3

You probably want to use something like:

countries.get(Math.abs(new Random().nextInt()) % countries.size());

Or, to avoid creating a new Random object every time, you could use the same one:

Random gen = new Random();
for (int i = 1; i < 10; i++) {
    System.out.println(countries.get(Math.abs(gen.nextInt()) % countries.size()));
}
Sign up to request clarification or add additional context in comments.

3 Comments

or .nextInt(countries.size()).
can we do like this countries.get(obj.nextInt()); ?
@AlexisC: That's true. You can do that, also. @AmanKumar: You can, but pay attention to avoid a potential indexOutOfBounds exception. You have to make sure that obj.nextInt() is positive and less than countries.size().
1

I would use Collections.shuffle(countries) if you wanted a randomized List.

Else a new Random().nextInt(max) like Flavius described.

Comments

1
static void shuffleArray(string[] ar)
  {
    //set the seed for the random variable
    Random rnd = ThreadLocalRandom.current();
    //go from the last element to the first one.
    for (int i = ar.size()- 1; i > 0; i--)
    {
      //get a random number till the current position and simply swap elements
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }

This way you shuffle the entire array and get the values in a random order but NO duplicate at all. Every single element changes position, so that no matter what element (position) you pick, you get a country from a random position. You can return the entire vector, the positions are random.

1 Comment

Could you comment it please? And by the way, I get the " Cannot resolve method 'length()' " error.

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.