Warz
Warz

Reputation: 7766

Proper looping construct using Arrays and List

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;
}

Upvotes: 0

Views: 112

Answers (5)

Akhi
Akhi

Reputation: 2242

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; 
    }

Upvotes: 0

sharakan
sharakan

Reputation: 6901

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;
}

Upvotes: 0

ioseb
ioseb

Reputation: 16951

I believe this needs to be changed from:

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

To:

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502116

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();
}

Upvotes: 4

Eng.Fouad
Eng.Fouad

Reputation: 117617

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;  
}

Upvotes: 3

Related Questions