# Combining 3 list into one

• 12-15-2011, 07:36 PM
Combining 3 list into one
I'm trying to combine three list which I pass into a single list

}

I know of the following approach but this doesnt work :

Any suggestions..

Thank you !
• 12-15-2011, 08:00 PM
Norm
Re: Combining 3 list into one
Rethink the way you are using the addAll method. That should be the method to use.
Do you want one list with all the elements from the three lists or do you want a list that contains the three lists?
• 12-15-2011, 08:15 PM
kevinn205
Re: Combining 3 list into one
one possiblity is to read each list with a loop then add them to a new list that contains all three

not the most efficent but it gets the job done
• 12-15-2011, 08:58 PM
Re: Combining 3 list into one
Quote:

Originally Posted by Norm
Rethink the way you are using the addAll method. That should be the method to use.
Do you want one list with all the elements from the three lists or do you want a list that contains the three lists?

The number of list's is variable, I require a list that contains the three lists (ConsolidatedList) which I can then pass as a paremeter to another method.

Thanks!
• 12-15-2011, 09:25 PM
2by4
Re: Combining 3 list into one
Quote:

I know of the following approach but this doesnt work :

Any suggestions..

Thank you !

Can you post the error you get for this?
• 12-15-2011, 09:38 PM
Norm
Re: Combining 3 list into one
Quote:

I require a list that contains the three lists
Are you asking for a list of lists? One list that has three elements.
• 12-15-2011, 09:59 PM
Re: Combining 3 list into one
Quote:

Originally Posted by Norm
Are you asking for a list of lists? One list that has three elements.

Yes that's exactly what I meant !
• 12-15-2011, 10:07 PM
Norm
Re: Combining 3 list into one
Define an ArrayList that is to hold ArrayList<Book> and add the three ArrayList<Book> to it.
ArrayList<ArrayList<Book>>
• 12-15-2011, 10:12 PM
Re: Combining 3 list into one
Quote:

Originally Posted by Norm
Define an ArrayList that is to hold ArrayList<Book> and add the three ArrayList<Book> to it.
ArrayList<ArrayList<Book>>

Thanks but the problem I'm facing is in accessing the arguments of the add method.

List<List<Book>> bookList = new ArrayList<List<Book>>();
• 12-15-2011, 10:18 PM
Norm
Re: Combining 3 list into one
Quote:

but the problem I'm facing is in accessing the arguments of the add method.
• 12-15-2011, 10:23 PM
Re: Combining 3 list into one
Method I use to make the call
public void getPeopleReadingBooks(15, fictionBooks, alienBooks, scienceBooks);

I cant compile the program as there is a red line on the arguments of the add method.

List<List<Book>> bookList = new ArrayList<List<Book>>();
}

Thanks Norm for your quick response !
• 12-15-2011, 10:25 PM
Norm
Re: Combining 3 list into one
Quote:

there is a red line
Sorry. A red line has no text.
You need to compile the code and get the compiler's error messages.
• 12-15-2011, 10:30 PM
Re: Combining 3 list into one
Quote:

Originally Posted by Norm
Sorry. A red line has no text.
You need to compile the code and get the compiler's error messages.

fictionBooks cannot be resolved to a variable.
alienBooks cannot be resolved to a variable.
• 12-15-2011, 10:31 PM
Norm
Re: Combining 3 list into one
Are the definitions for those variable in scope where you are trying to use them?
• 12-15-2011, 10:39 PM
Re: Combining 3 list into one
The values get passed but its not in the scope so I'm not sure how to access them. This is a huge application, build time is like 6 minutes.

Can I write something like

• 12-15-2011, 10:49 PM
Norm
Re: Combining 3 list into one
Quote:

Can I write something like

When in doubt ask the compiler by trying to compile it.
• 12-15-2011, 10:50 PM