Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ARRAYLIST

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question ARRAYLIST

    Hi,
    I need to create a method which selects a random word from my array list at random...at the moment it prints out what is in the array list...

     
    public class Bank
     
    {   
       public static void main(String[] args)   
       {   
          String[] words = {"Garage", "Swindon", "Newspaper", "Chocoloate"};   
     
          List<String> wordList = Arrays.asList(words);   
     
          for (String e : wordList)   
          {   
             System.out.println(e);   
     
            }
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: ARRAYLIST

    You need to actually generate the random number and only print out the specific element.

    To generate a random number:
    Random Class

    To print out a specific element of an array list:
    System.out.println(myArrayList.get(index));

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ARRAYLIST

    It comes up with cannot find symbol variable index?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: ARRAYLIST

    You have to declare the variable and set it with the result you get from using the Random class.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ARRAYLIST

    Further to helloworld922's contribution,

    Assuming 'generator' is an instance of

    java.util.Random

    int index = generator.nextInt( n ), where 'n' is the length of your arraylist.

    Then print out the arraylist element at the position, 'index'.

    I hope this helps.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM
  3. Iterator, with ArrayList
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: October 25th, 2009, 09:00 AM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM