-1

Alright, I keep trying to oogle this but It keeps showing answers with a string array which I can't use. This is currently my code:

    List<String> Shuffle = new ArrayList<String>();
    if(EAmount == finalk){
        Shuffle.add("Emerald");
    }
    if(DAmount == finalk){
        Shuffle.add("Diamond");
    }
    if(GAmount ==finalk){
        Shuffle.add("Gold");
    }
    if(IAmount == finalk){
        Shuffle.add("Iron");
    }

I can't find out how to get a random string from Shuffle. Please help! BTW: The Amounts and finalk are integers

  • 3
    What are `EAmount` and `finalk`? – Sotirios Delimanolis Apr 27 '14 at 17:29
  • 4
    It's really unclear where you expect any randomness in here... indeed, it's unclear how this code is relevant at all. I suggest you try to understand the answers which use an array, and then apply that knowledge to lists... – Jon Skeet Apr 27 '14 at 17:31

2 Answers2

1

You can generate a random integer between zero and the size of the collection (exclusive), and then use it as an index to refer to the corresponding item of the collection.

int randIndex = new Random().nextInt(Shuffle.size()); //generate rand int [0, size[
String randString = Shuffle.get(randIndex); //get random string
WoDoSc
  • 2,598
  • 1
  • 13
  • 26
0

It is the same as with Arrays, you just need to use the get Method:

Shuffle.get(random)
AHH
  • 981
  • 2
  • 10
  • 26