HashMap help

Discussion in 'Plugin Development' started by TehVoyager, Nov 3, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    TehVoyager

    In my plugin I get the size of a hashmap and pick a random number so say my hashmap is like this
    Code:
    (1) Notch, Good Guys
    (2) Jeb, Good Guys
    (3) Ect, Good Guys
    
    and the random number is 2, I want to get the row 2(Jeb) and set his thing to badguy. Thanks
     
  2. Offline

    OffLuffy

    I would think the Random object in Java would be good for this, although not with the HashMap, but rather a ArrayList with a series of player names in it.
    Assuming that there's a ArrayList<String> object called 'list' with the data above in it, we could do this:
    Code:java
    1. Random r = new Random();
    2. int num = r.nextInt(list.size());
    3. String badguy = list.get(num);

    Then the bad guy could be stored in a variable somewhere. Reason the HashMap wouldn't work is because a HashMap can't fetch a name by index as a list could. The Random object's nextInt(int) method fetches a int between 0 and the number specified, this also includes 0, so the first entry of the list won't be ignored.
     
Thread Status:
Not open for further replies.

Share This Page