Reputation: 1708
I am trying to part the mainList
arraylist to 3 sublist Arraylist and then add it as sublist in other arraylist mainSublist
but I am getting this error:
How can I fix it?
I appreciate any help.
1)-Type mismatch: cannot convert from List to ArrayList
2) -The method add(RootCreator) in the type ArrayList is not applicable for the arguments (ArrayList)
Code:
ArrayList<RootCreator> mainList = new ArrayList<RootCreator>();
for (String key : names) {
RootCreator rootcreat = join_line(path, key);
mainList.add(rootcreat);
}
ArrayList<RootCreator> mainSublist = new ArrayList<RootCreator>();
for(int i= 0 ; i < mainList.size(); i++){
int index = i*3;
//the error 1 is here
ArrayList<RootCreator> sublist = mainList.subList(0, index);
//error 2 is here
mainSublist.add(sublist);
}
Upvotes: 1
Views: 951
Reputation: 9334
OK, let's look at the two errors here:
//the error 1 is here
ArrayList<RootCreator> sublist = mainList.subList(0, index);
This is telling you that you're trying to assign something of the type List
to a variable that's declared as an ArrayList
. While an ArrayList
is a subtype of List
, it doesn't work both ways - you can assign an ArrayList
to a List
, but not vice versa. So let's change your declaration:
//the error 1 was here, but fixed.
List<RootCreator> sublist = mainList.subList(0, index);
The second one is slightly different:
//error 2 is here
mainSublist.add(sublist);
Here, you've got a list defined to hold items of the type RootCreator
- but you're not adding RootCreator
items, you're adding a list of them. So if you want to have the list hold other lists, you need to specify that when you create it:
ArrayList<List<RootCreator>> mainSublist = new ArrayList<List<RootCreator>>();
But, if you're trying to just keep a single list and want to add everything from the sublists, then instead, change your code to:
//error 2 was here, but Fixed
mainSublist.addAll(sublist);
Upvotes: 0
Reputation: 3592
All List are not ArrayList, so when you use sublist you get a generic List. On the other hand, the method to add a collection to another collection is addAll instead add.
List<RootCreator> mainList = new ArrayList<RootCreator>();
for (String key : names) {
RootCreator rootcreat = join_line(path, key);
mainList.add(rootcreat);
}
List<RootCreator> mainSublist = new ArrayList<RootCreator>();
for(int i= 0 ; i < mainList.size(); i++){
int index = i*3;
//the error 1 is here
List<RootCreator> sublist = mainList.subList(0, index);
//error 2 is here
mainSublist.addAll(sublist);
}
Upvotes: 1
Reputation: 178253
The subList
method returns a List
, not an ArrayList
, and you cannot assign a List
to an ArrayList
. In fact, the List
returned by the subList
method is not an ArrayList
.
If it needs to be an ArrayList
, then create an ArrayList
out of the sub list.
ArrayList<RootCreator> sublist = new ArrayList<>(mainList.subList(0, index));
If you just need a List
, then make sublist
a List
instead.
List<RootCreator> sublist = mainList.subList(0, index);
You can't add an ArrayList<RootCreator>
to an ArrayList<RootCreator>
; you must add a RootCreator
. If you want to add the elements of sublist
to another list, then use the addAll
method.
mainSublist.addAll(sublist);
Upvotes: 0