-
[Java]Array List
Ok i need help grabbing a random string from an arraylist.
I declared:
Code:
public static ArrayList<String> cZP = new ArrayList<String>();
And added this to the appropriate spotIt adds it to the list fine, but I'm having trouble making it randomly select 1 of the usernames added to the list. I've tried a few things but keep getting errors.
-
Re: [Java]Array List
You could try creating a random number between 0 and cZP.size(), then call cZP.get(number) using that random number - shouldn't give you errors really. What have you tried already and why did it not work?
-
Re: [Java]Array List
Code:
int r = random(0, cZP.length);
string p = cZP[r];
should work?
-
Re: [Java]Array List
Quote:
Originally Posted by
UberMegaFly
Code:
int r = random(0, cZP.length);
string p = cZP[r];
should work?
That won't work, cZP[r] will give a compile-time error, you would need to use something like cZP.get(r), and also random() would need to be a separate method.
-
Re: [Java]Array List
Try something like this.
Code:
ArrayList<String> cZP = new ArrayList<String>();
cZP.add("name");
cZP.add("name2");
...
int r = new Random().nextInt(cZP.size());
String name = cZP.get(r);
-
Re: [Java]Array List
cZP.get(Math.Random(cZP.size()));
Shoould work, mate