# Thread: Combining 3 list into one

1. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## 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 !

2. ## 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?

3. Member
Join Date
Nov 2011
Posts
65
Rep Power
0

## 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

4. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## Re: Combining 3 list into one

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!

5. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## Re: Combining 3 list into one

I know of the following approach but this doesnt work :

Any suggestions..

Thank you !
Can you post the error you get for this?

6. ## Re: Combining 3 list into one

I require a list that contains the three lists
Are you asking for a list of lists? One list that has three elements.

7. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## Re: Combining 3 list into one

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 !

8. ## 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>>

9. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## Re: Combining 3 list into one

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

10. ## Re: Combining 3 list into one

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

11. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## 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. ## Re: Combining 3 list into one

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.

13. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## Re: Combining 3 list into one

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.

14. ## Re: Combining 3 list into one

Are the definitions for those variable in scope where you are trying to use them?

15. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## 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

16. ## Re: Combining 3 list into one

Can I write something like

When in doubt ask the compiler by trying to compile it.

17. Member
Join Date
Jul 2010
Posts
18
Rep Power
0

## Re: Combining 3 list into one

I think this is it ! Thanks Norm and everyone for all your posts !

List<List<Book>> consbook = new ArrayList<List<Book>>();

for ( List<Book> b : bookLists ){