0

In this simple example, i want to create a String array populated with each person's first and last in my DB. I know i am missing something very obvious as i keep overriding i in the following looping method. A second eye would certainly help.

    /**
 * 
 * @return
 */
public String[] buildFullNameContainer(){

    List<Person> allPeople = Person.findAllPeople();
    String[] peopleContainer = new String[] {""};
    String fullName = "";



        for (int i = 0; i < peopleContainer.length; i++) {
            for (Person person : allPeople) {
                fullName = person.getFirstName() + " " + person.getLastName();  
                peopleContainer[i] = fullName; 

            }
        }
    return peopleContainer;
}
3
  • What exactly is wrong? Does something not work? Commented Jun 14, 2012 at 20:11
  • There is only one value when i loop through the String array after calling this method Commented Jun 14, 2012 at 20:12
  • 3
    @Warz: How would you expect there to ever be more than one element? You're creating an array with one element to start with, and arrays never expand... Commented Jun 14, 2012 at 20:14

5 Answers 5

4

Your array always has a single element - you should create it to be the same length as your list. Additionally, you've got two nested loops for no reason, and I see no point in the fullName variable. Here's the code I'd use:

String[] peopleContainer = new String[allPeople.size()];

for (int i = 0; i < peopleContainer.length; i++) {
    Person person = allPeople.get(i);
    peopleContainer[i] = person.getFirstName() + " " + person.getLastName();
}
Sign up to request clarification or add additional context in comments.

Comments

3

Replace

String[] peopleContainer = new String[] {""};

with

String[] peopleContainer = new String[allPeople.size()];

Also, edit your loop as follows:

for(int i = 0; i < peopleContainer.length; i++)
{
    Person person = allPeople.get(i);
    fullName = person.getFirstName() + " " + person.getLastName();  
    peopleContainer[i] = fullName;  
}

3 Comments

if i remove the outter for loop, how can i reference 'i' as it does not exist
Initialize an int i at the beginning. In the peopleContainer[i] = fullName; line, change the i to a i++.
Best answer as it gets rid of the unnecessary inner for-loop. will accept in a few
0

I believe this needs to be changed from:

String[] peopleContainer = new String[] {""};

To:

String[] peopleContainer = new String[allPeople.size()];

Comments

0

You're looping over your result array, instead of your source data collection. You have to size the array correctly first, then loop over the size of either the array or the list. No need for a nested for.

public String[] buildFullNameContainer(){

    List<Person> allPeople = Person.findAllPeople();
    String[] peopleContainer = new String[allPeople.size()];
    for (int i = 0; i < peopleContainer.length; i++) {
        Person person = allPeople.get(i);
        String fullName = person.getFirstName() + " " + person.getLastName();  
        peopleContainer[i] = fullName; 
    }
return peopleContainer;
}

Comments

0

Try This.

    List<Person> allPeople = Person.findAllPeople();
    String[] peopleContainer = new String[allPeople.size()];

    for (int i=0;i < allPeople.size();i++;) {
          Person person=allPeople.get(i);
          fullName = person.getFirstName() + " " + person.getLastName();  
          peopleContainer[i] = fullName; 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.