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

Thread: Array Lists filling for loop

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array Lists filling for loop

    Hi I am having trouble with Array Lists. What should the for loop header be for filling Array Lists?
    I am trying to use an explicit value Constant Names in the condition.
    Any help would be greatly appreciated. Thank you.
    Here is my code.


    <       public class PersonTester
    {
     
      public static void main(String[] args)
      {
       PersonTester tester = new PersonTester();
       // ArrayList<Person> persons = new ArrayList<Person>(20);
       String ELEMENT = "";
     
        for (int i = 0;i<ELEMENT.length(); i++)
        {
          char gender = tester.getRandomGender();
          String fName = tester.getRandomFirstName(gender);
          String lName = tester.getRandomLastName();
          int age = tester.getRandomAge();
          ArrayList<Person> persons = new ArrayList<Person>(20);
          persons[i] = new Person(fName, lName, gender, age);
          persons.size();
     
          //ArrayList<Person>people = new ArrayList<Person>(fName, lName, gender, age);
          System.out.println(persons);
        }
      }
     
      private char getRandomGender()
      {
        return Math.random() > 0.5? 'f':'m';
      }
     
      private String getRandomFirstName(char gender)
      {
        String firstName = "";
     
        if (gender == 'f')
        {
          firstName = femaleFirstNames[SingleRandom.getInstance().
                                       nextInt(femaleFirstNames.length)];
        }
        else
        {
          firstName = maleFirstNames[SingleRandom.getInstance().
                                     nextInt(maleFirstNames.length)];
        }
        return firstName;
      }
     
      private String getRandomLastName()
      {
        return lastNames[SingleRandom.getInstance().nextInt(lastNames.length)];
      }
     
      private int getRandomAge()
      {
        return SingleRandom.getInstance().nextInt(99) + 1;
      }                                                                                                        >
    Last edited by rob17; March 12th, 2012 at 09:26 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Array Lists filling for loop

    for filling Array Lists?
    What is the source of data that is being stored in the ArrayList?
    That could determine what controls the looping.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists filling for loop

    Thank you for posting back to me. I really appreciate it.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists filling for loop

    I am a new user how do I highlight my code when I re- edit it. Thank you.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Array Lists filling for loop

    Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting

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

    Default Re: Array Lists filling for loop

    Hey Norm I have not wrapped any code on this site before. I would Really appreciate it if you could tell me the
    step by step process on how to do it. Thank you.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Array Lists filling for loop

    1)Edit your post
    2) put the following before your code
    [code=java]
    3) Put<YOUR CODE HERE>
    4)Add the following after the code
    [/code]

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists filling for loop

    Thank you Norm I think I wrapped the code properly. It should be easier to read.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Array Lists filling for loop

    The loop should execute until the user is done entering data.
    Its up to you to determine when that is. For example:
    1) ask the user for the number of inputs he'll do and use that in a for statement
    2) tell the user to enter data for xx items then loop xx times
    3) have the user enter "DONE" or some thing to tell the program to exit the loop

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array Lists filling for loop

    Thank you for the help I really appreciate it. I did not think of that approach using the for loop. That makes sense. Thank you so much.

Similar Threads

  1. [SOLVED] Filling a 2D array with other arrays.
    By mwebb in forum Object Oriented Programming
    Replies: 4
    Last Post: March 1st, 2012, 04:11 PM
  2. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 04:12 PM
  3. filling an array with strings
    By exose in forum Collections and Generics
    Replies: 3
    Last Post: February 18th, 2011, 08:44 AM
  4. Array Lists help!!
    By lilika in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 4th, 2011, 08:21 AM
  5. Filling an Array?
    By Bascotie in forum Collections and Generics
    Replies: 5
    Last Post: October 14th, 2009, 06:27 PM