Answers for "get random from arraylist java"

4

random.choice in java

String[] available_cards = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Queen", "King", "Jack", "Ace"};  
java.util.Random random = new java.util.Random();
int random_computer_card = random.nextInt(available_cards.length);
System.out.println(available_cards[random_computer_card]);
Posted by: Guest on September-14-2020
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
0

get random String from array list

Random r = new Random();

    int randomitem = r.nextInt(myList.size());
    String randomElement = myList.get(randomitem);
Posted by: Guest on November-22-2020

Code answers related to "get random from arraylist java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language