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.
Code java:
< 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;
} >
Re: Array Lists filling for loop
Quote:
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
Re: Array Lists filling for loop
Thank you for posting back to me. I really appreciate it.
Re: Array Lists filling for loop
I am a new user how do I highlight my code when I re- edit it. Thank you.
Re: Array Lists filling for loop
Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting
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.
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]
Re: Array Lists filling for loop
Thank you Norm I think I wrapped the code properly. It should be easier to read.
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
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.