Reputation: 33
Here is my code:
import java.util.Collections;
import java.util.*;
public class ListMerge<String> extends LinkedList<String>
{
public LinkedList<String> mergeLists(LinkedList<String> one, LinkedList<String> two)
{
LinkedList<String> newList = new LinkedList<String>();
newList.addAll(one);
newList.addAll(two);
newList = Collections.sort(newList);
return newList;
}
}
I get the error "cannot find symbol - method sort(java.util.LinkedList), and I am completely unaware as to why it is not working. I've imported the collections class and in the in-line coding I've called Collections, so I'm not sure why it can't seem to find the method.
I'm a beginner in coding so my vocabulary in coding isn't too strong, so please accommodate!
Thank you!
Upvotes: 0
Views: 1020
Reputation: 280179
Your issue after changing to java.util.Collections
is the following
v this is a type argument
public class ListMerge<String> extends LinkedList<String>
^ this is a type parameter
The type parameter you have in ListMerge<String>
, ie. <String>
is the simple name of the parameter. It has nothing to do with java.lang.String
. As such you are obscuring the type java.lang.String
.
This might actually be what you wanted in the first place, ie. you planned to have ListMerge
work for all types, equivalent to
public class ListMerge<T> extends LinkedList<T>
If it isn't, then what you want is
public class ListMerge extends LinkedList<String>
Go through the official Java generics tutorial to understand more about the syntax of generic class declarations.
Upvotes: 0
Reputation: 4500
If you're intending on extending LinkedList, you have to extend the generic class "LinkedList... you can't extend a version of a generic class with the type specified: "LinkedList". You only specify a generic class's template arguments "with String, for instance" when you instantiate an instance of it
LinkedList<String> list = new LinkedList<String>()
You might find it simpler, however, to have a ListMerger class, that has a method "mergeLists" which returns a LinkedList object.
public class ListMerger<E extends Comparable>
{
public LinkedList<E> mergeLists(LinkedList<E> one, LinkedList<E> two)
{
LinkedList<E> newList = new LinkedList<E>();
newList.addAll(one);
newList.addAll(two);
Collections.sort(newList);
return newList;
}
}
And as others have mentioned, yes, the Collections.sort method modifies the list you input. It doesn't return a sorted copy of it. So see how I did it in my code. Then, all you have to do to get your merged LinkedList is this:
ListMerger<String> merger = new ListMerger<String>();
LinkedList<String> mergedList = merger.mergeLists(one, two);
Notice how I specified "String" when I was USING the generic class. Not defining it. Oh, and notice I changed the name from ListMerge to ListMerger. Just personal preference I guess.
EDIT: Updated the example to require E to be "Comparable". That way, Collections.sort() will accept it.
Upvotes: 0
Reputation: 4987
Watch out for return type: void sort(List<T> list)
, it sorts given list.
Compiler expects method with signature LinkedList<String> sort(List<T> list)
, which is not exist in Collections
class.
This line is illegal:
newList = Collections.sort(newList);
You could easily fix that:
Collections.sort(newList);
Upvotes: 3