Answers for "pick random item from arraylist java"

0

random item from arraylist

ArrayList<Type> arraylistName = new ArrayList<>();

// add items to arraylist
arraylistName.add(item1);
arraylistName.add(item2);
arraylistName.add(item3);

Random random = new Random();
// random index between 0 and arraylistName.size();
int randomIndex = random.nextInt(arraylistName.size());

System.out.println(arraylistName.get(randomIndex));
Posted by: Guest on January-22-2021

Code answers related to "pick random item from arraylist java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language