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 17 of 17

Thread: ArrayList Problem

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList Problem

    Alright so far i've created the list, and am able to add/delete certain things from the list when neccessary.

    Heres what I have..

    public static ArrayList<String> cSP = new ArrayList<String>();
    cSP.add(c.playerName);


    Now my problem is, I need to be able to pull a random String from inside the ArrayList, i've searched around but can't find out how.

    Thanks,
    Marty


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: ArrayList Problem

    Hi Marty,

    Welcome to the java programming forums.

    Can you pls give all your codes so we can figure it out.

    Thanks.

    Cheers,
    Truffy

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Problem

    Quote Originally Posted by Truffy View Post
    Hi Marty,

    Welcome to the java programming forums.

    Can you pls give all your codes so we can figure it out.

    Thanks.

    Cheers,
    Truffy
    Thats basically all i have..

    It adds it to the list fine as i had it print the list and it showed the playerName after it was added, just i need to add a method that picks a random String from inside of the list.

  4. #4
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: ArrayList Problem

    Quote Originally Posted by Marty View Post
    Thats basically all i have..

    It adds it to the list fine as i had it print the list and it showed the playerName after it was added, just i need to add a method that picks a random String from inside of the list.
    heres a sample code that might help.

    import java.util.ArrayList;
     
    public class Main {
      public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
     
        arrayList.add(1, "INSERTED");
        for (String str: arrayList)
          System.out.println(str);
      }
    }

    output:
    /*
    1
    INSERTED
    2
    3
    */

    hope it helps because in that you ca add a method to randomlu pull the list youd added.

    Cheers,
    Truffy

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Problem

    Quote Originally Posted by Truffy View Post
    heres a sample code that might help.

    import java.util.ArrayList;
     
    public class Main {
      public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
     
        arrayList.add(1, "INSERTED");
        for (String str: arrayList)
          System.out.println(str);
      }
    }

    output:
    /*
    1
    INSERTED
    2
    3
    */

    hope it helps because in that you ca add a method to randomlu pull the list youd added.

    Cheers,
    Truffy
    I don't want to pull the list, I want to pull a random string from the list.

  6. #6
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: ArrayList Problem

    can u explain fartehr what you want to try. im sorry got lost.

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: ArrayList Problem

    Hello Marty,

    Welcome to the Java Programming Forums

    I believe this is what you are looking for:

    import java.util.ArrayList;
    import java.util.Random;
     
    public class Marty {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add(0, "Java");
            arrayList.add(1, "Programming");
            arrayList.add(2, "Forums");
            arrayList.add(3, ".com");
     
            // Generate random number 0 - 3
            Random random = new Random();
            int rand = random.nextInt(4);
     
            // Convert ArrayList to Object array
            Object[] elements = arrayList.toArray();
     
            // Print array element at random number
            System.out.println(elements[rand]);
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: ArrayList Problem

    Quote Originally Posted by JavaPF View Post
    Hello Marty,

    Welcome to the Java Programming Forums

    I believe this is what you are looking for:

    import java.util.ArrayList;
    import java.util.Random;
     
    public class Marty {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add(0, "Java");
            arrayList.add(1, "Programming");
            arrayList.add(2, "Forums");
            arrayList.add(3, ".com");
     
            // Generate random number 0 - 3
            Random random = new Random();
            int rand = random.nextInt(4);
     
            // Convert ArrayList to Object array
            Object[] elements = arrayList.toArray();
     
            // Print array element at random number
            System.out.println(elements[rand]);
        }
    }
    great post Sir. Shame i didn't understand what he meant. Anyway, thanks.

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: ArrayList Problem

    Thanks Truffy. Thank you for your efforts too
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  10. #10
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: ArrayList Problem

    Quote Originally Posted by JavaPF View Post
    Thanks Truffy. Thank you for your efforts too
    Your welcome.

    Cheers,
    Truffy

  11. #11
    Junior Member
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Problem

    The only thing I would add to this is to put the random generator into a function/method so that you can continually recall the random function.You could even put the whole thing into a while loop so that while a certain condition is true it continually pulls random positions from the ArrayList. I would even seed the random function using the system time. Happy Programming.

  12. #12
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: ArrayList Problem

    Spam reported

    db

  13. #13
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: ArrayList Problem

    Sorted, thanks Darryl.Burke

    // Json

  14. #14
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ArrayList Problem

    I would do it a little differently.

    import java.util.ArrayList;
    import java.lang.Math;
     
    public class Marty {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add(0, "Java");
            arrayList.add(1, "Programming");
            arrayList.add(2, "Forums");
            arrayList.add(3, ".com");
     
            // Generate random number 0 - 3
            int rand = (int)(Math.random()*4);
     
            // Print array element at random number
            System.out.println(arrayList.get(rand));
        }
    }

    I'll explain what I did:

     int rand = (int)(Math.random()*4);
    This will return a random number between 0 and 3. The Math.random() method returns a random double greater than or equal to 0 and less than 1. So, if we multiply the result by 4, it will give us a random number greater than or equal to 0 and less than 4. BUT, we also have to cast it as an int because the ArrayList.get(int n) method doesnt accept a double. So to convert a double to an int, we simply say (int)(double number) and that turns our double to an int by cutting off the decimal.

    System.out.println(arrayList.get(rand));
    This will use the ArrayList's predefined method to get an element of the arraylist based on the index number. This way we dont have to move our elements into an array.

  15. #15
    Junior Member
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: ArrayList Problem

    try:

    int rand=(int)(Math.random()*ArrayListName.size());
    System.out.println(ArrayListName.get(rand));

  16. #16
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: ArrayList Problem

    Quote Originally Posted by cosecant View Post
    try:

    int rand=(int)(Math.random()*ArrayListName.size());
    System.out.println(ArrayListName.get(rand));
    Ya, I thought about that afterwards. That is a better one because ArrayListName.size() will change the range of the random numbers when you add more items to the array.

  17. #17
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ArrayList Problem

    Spam reported

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. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  3. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM

Tags for this Thread