user2639540
user2639540

Reputation: 35

Get a random text from ArrayList

I'm just learning Java and I'm having a problem with ArrayList or retrieving a random line of text. What I'm trying to achieve is a random quote (from a list) to load on every page refresh. Never done an Array/Random before, not sure if this is the right method?

Here's what's working so far: https://jsfiddle.net/5wryh23L/

code that's not working:

 Random random = new Random(); 
 int index = random.nextInt(10);


    List<String> list = new ArrayList<String>();
    list.add("quote1");
    list.add("quote2");
    list.add("quote3");
    list.add("quote4");
    list.add("quote5");

    RandomExample obj = new RandomExample();
    for(int i = 0; i < 10; i++){
        System.out.println(obj.getRandomList(list));
    }

}

getRandomList(List<String> list) {

    //0-4
    int index = random.nextInt(list.size());
    System.out.println("\nIndex :" + index );
    return list.get(index);

}

Upvotes: 0

Views: 1929

Answers (3)

Linux Le
Linux Le

Reputation: 456

    This example gives how to shuffle elements in the ArrayList. By calling Collections.shuffle() method you can shuffle the content of the ArrayList. Every time you call shuffle() method, it generates different order of output. 

        **Example #1**

            public class MyListShuffle {

                public static void main(String a[]){
                    ArrayList<String> wordList = new ArrayList<String>();
                    list.add("Stack");
                    list.add("Overflow");
                    list.add("is");
                    list.add("a");
                    list.add("place");
                    list.add("great");
                    list.add("to";
                    list.add("learn");
                    list.add("and");
                    list.add("teach");



                    Collections.shuffle(wordList);
                    System.out.println("Results after shuffle operation #1:");
                    for(String word: wordList){
                        System.out.println(word);
                    }


                    Collections.shuffle(wordList);
                    System.out.println("Results after shuffle operation #2:");
                    for(String word: wordList){
                        System.out.println(word);
                    }
                }
            }

        Note that The shuffle(List<?>) method is used to randomly permute the specified list using a default source of randomness.

    The original declaration:
    -  java.util.Collections.shuffle() method.  
         - public static void shuffle(List<?> list)
            - Parameter: "list" is the list to be shuffled.

Output:
Results after shuffle operation #1:
to 
learn
and
Stack
Overflow
a
teach 
is
great
place
Results after shuffle operation #2:
Overflow
place
great 
and
teach
to
is
learn
Stack
a

Example #2

  public class ShuffleList {
           public static void main(String args[]) {
           // create an array list object       
           List myList = new ArrayList();

           // populate the list
           myList.add("A");
           myList.add("B");
           myList.add("C"); 
           myList.add("D");
           myList.add("E"); 

           System.out.println("Collection after: "+ myList);

           // shuffle the list
           Collections.shuffle(myList);

           System.out.println("Collection after shuffle: "+ myList);
           }    
        } 

After compiling the program, the output is:

Collection before shuffle: [A, B, C, D, E]
Collection after shuffle: [C, A, E, B, D]

Or you can return as following

Example #3

    return list.get(random.nextInt(list.size()));

Upvotes: 0

Tuco
Tuco

Reputation: 128

Your random methods is apparently correct and the way you are structuring your list also. I believe your problem is because you don't have a main method do you? If you create the list inside a main method and call the getRandomList() inside the main method it will works. It will be more or less like this:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


static Random random = new Random(); 
    int index = random.nextInt(10);

    public static void main(String args[]){
        List<String> list = new ArrayList<String>();
        list.add("quote1");
        list.add("quote2");
        list.add("quote3");
        list.add("quote4");
        list.add("quote5");

        // RandomExample obj = new RandomExample
        for(int i = 0; i < 10; i++){
            System.out.println(getRandomList(list));
        }

    }



    static String getRandomList(List<String> list) {

        //0-4
        int index = random.nextInt(list.size());
        System.out.println("Index :" + index );
        return list.get(index);

    }

Upvotes: 0

BoDidely
BoDidely

Reputation: 504

A really easy way to get a random item from your list:

list.get((int)(Math.random()*list.size()));

Upvotes: 2

Related Questions