Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- public class Loop {
- public static void main(String[] args) {
- // Create ArrayList and Random
- ArrayList<String> list = new ArrayList<String>();
- Random rand = new Random();
- // Add values to ArrayList list
- list.add("hi");
- list.add("World");
- list.add("Greetings");
- list.add("Jupiter");
- list.add("Riddle");
- // Loop 5 times
- for(int i=0;i<5;i++) {
- // Randomly select a number based on the number of iterations the loop has run through
- int n = rand.nextInt(5-i);
- // Print that index to console and remove from the ArrayList list
- System.out.println(list.get(n));
- list.remove(n);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment