Results 1 to 17 of 17
Thread: Combining 3 list into one
- 12-15-2011, 06:36 PM #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
getPeopleReadingBooks(15, fictionBooks, alienBooks, scienceBooks);
public Map<String, List<BookUser>> getPeopleReadingBooks(int bookReaders, List<Book>... bookLists){
}
I know of the following approach but this doesnt work :
List<Book> newList = new ArrayList<Book>() { { addAll(fictionBooks); addAll(alienBooks); addAll(scienceBooks) } };
Any suggestions..
Thank you !
- 12-15-2011, 07:00 PM #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?
- 12-15-2011, 07:15 PM #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
- 12-15-2011, 07:58 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
- 12-15-2011, 08:25 PM #5
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-15-2011, 08:38 PM #6
Re: Combining 3 list into one
Are you asking for a list of lists? One list that has three elements.I require a list that contains the three lists
- 12-15-2011, 08:59 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
- 12-15-2011, 09:07 PM #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>>
- 12-15-2011, 09:12 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
- 12-15-2011, 09:18 PM #10
Re: Combining 3 list into one
What problem? Please explain.but the problem I'm facing is in accessing the arguments of the add method.
Please post the full text of your errors.
- 12-15-2011, 09:23 PM #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.
public void getPeopleReadingBooks(int bookReaders, List<Book>... bookLists){
List<List<Book>> bookList = new ArrayList<List<Book>>();
bookList.add(fictionBooks);
bookList.add(alienBooks);
bookList.add(scienceBooks);
}
Thanks Norm for your quick response !
- 12-15-2011, 09:25 PM #12
Re: Combining 3 list into one
Sorry. A red line has no text.there is a red line
You need to compile the code and get the compiler's error messages.
- 12-15-2011, 09:30 PM #13
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
- 12-15-2011, 09:31 PM #14
Re: Combining 3 list into one
Are the definitions for those variable in scope where you are trying to use them?
- 12-15-2011, 09:39 PM #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.
public void getPeopleReadingBooks(int bookReaders, List<Book>... bookLists){
Can I write something like
bookList.add(List <Book> bookLists);
- 12-15-2011, 09:49 PM #16
Re: Combining 3 list into one
Have you read the API doc for the add() method? What is the data type of your args to the add method?Can I write something like
When in doubt ask the compiler by trying to compile it.
- 12-15-2011, 09:50 PM #17
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Combining shapes?
By snj00u in forum Java 2DReplies: 2Last Post: 06-14-2011, 09:09 PM -
Java combining arrays
By trakke in forum New To JavaReplies: 5Last Post: 04-28-2011, 09:32 PM -
Combining these 2 projects
By fresh83 in forum New To JavaReplies: 14Last Post: 12-28-2009, 08:52 AM -
combining two separate codes into one
By jaiminparikh in forum Advanced JavaReplies: 22Last Post: 02-18-2009, 05:35 PM -
Help combining loops into 1 program.
By kewlgeye in forum New To JavaReplies: 5Last Post: 04-22-2008, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks