askIT

Pick random from array

Use the array.length to get the size of the array. Then pick a number from 0 to the size with Random().nextInt.

String[] countries = {"denmark","norway","sweden"};
int randomCountryNumber = new Random().nextInt(countries.length);
System.out.println(countries[randomCountryNumber]);

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.