I'm studying java in my class and I need to sort a doubly linked list, and I got the code, it works, but when I use the code in my environment, I receive an error:
The method sort(DoubleLinkedList<T>) in the type DoubleLinkedListSort is not applicable for the arguments (DoubleLinkedList<Item>)
This is the class that I got here: https://stackoverflow.com/a/2938529/4611395
Just changed the List class to my DoubleLinkedList class. So, I'm using this way.
DoubleLinkedList<Item> it = new DoubleLinkedList<Item>();
it = DoubleLinkedListSort.sort( it );
If I change the generic T to my class Item, it works with no problem. Anyone know why this problem? I learned that it should be working.
Thank you.
Edit:
This is my DoubleLinkedListSort that I edited.
package list;
import list.DoubleLinkedList;
public class DoubleLinkedListSort
{
private static int depth = 0;
public static <T extends Comparable<T>> DoubleLinkedList<T> sort( DoubleLinkedList<T> Ts )
{
depth++;
String tabs = getTabs();
System.out.println( tabs + "Sorting: " + Ts );
if( Ts.listSize() <= 1 )
{
depth--;
return Ts;
}
DoubleLinkedList<T> left = new DoubleLinkedList<T>();
DoubleLinkedList<T> right = new DoubleLinkedList<T>();
DoubleLinkedList<T> result = new DoubleLinkedList<T>();
int middle = Ts.listSize() / 2;
int added = 0;
for( int i = 0; i < Ts.listSize(); i++ )
{
try
{
if( added++ < middle )
left.insertFirst( Ts.getElementAtPosition( i ) );
else
right.insertFirst( Ts.getElementAtPosition( i ) );
}
catch( DoubleLinkedListException d )
{
}
}
left = sort( left );
right = sort( right );
result = merge( left, right );
System.out.println( tabs + "Sorted to: " + result );
depth--;
return result;
}
private static <T extends Comparable<T>> DoubleLinkedList<T> merge( DoubleLinkedList<T> left, DoubleLinkedList<T> right )
{
String tabs = getTabs();
System.out.println( tabs + "Merging: " + left + " & " + right );
DoubleLinkedList<T> result = new DoubleLinkedList<T>();
while( left.listSize() > 0 && right.listSize() > 0 )
{
try
{
if( left.getElementAtPosition( 0 ).compareTo( right.getElementAtPosition( 0 ) ) < 0 )
{
result.insertLast( left.getElementAtPosition( 0 ) );
left.deleteElementAtPosition( 0 );
}
else
{
result.insertLast( right.getElementAtPosition( 0 ) );
right.deleteElementAtPosition( 0 );
}
}
catch( DoubleLinkedListException d )
{
}
}
try
{
if( left.listSize() > 0 )
{
for( int i = 0; i < left.listSize(); i++ )
{
result.insertLast( left.getElementAtPosition( i ) );
}
}
else
{
for( int i = 0; i < right.listSize(); i++ )
{
result.insertLast( right.getElementAtPosition( i ) );
}
}
}
catch( DoubleLinkedListException d )
{
}
return result;
}
private static String getTabs()
{
StringBuffer sb = new StringBuffer( "" );
for( int i = 0; i < depth; i++ )
sb.append( '\t' );
return sb.toString();
}
}
Edit 2:
As Method with generics don't accept class as argument and https://stackoverflow.com/a/30061121/4611395 commented, it was just implement Comparable and it worked.
public class Item implements Comparable<Item>
.
.
.
@Override
public int compareTo( Item it )
{
return this.name.compareTo( it.name );
}
Thank you guys for the help.