How do I correctly implement Comparable for List in this instance?

风格不统一 提交于 2019-12-11 10:39:42

问题


What I want to do is use the compareTo() method to compare removedItemFromList1 and removedItemFromList2, then insert whichever value is smaller into the modifiedList3 and put the larger of the two removedFromList 1 or 2 back into its original list. If I had hair long enough to pull, it would have been pulled out by now...Am I not casting correctly? How do I correctly go about using the compareTo() method to accomplish this?

public class List<Integer> implements Comparable 
{
    private ListNode<Integer> firstNode;
    private ListNode<Integer> lastNode;


    public void insertAtBack (Integer insertItem)
    {
        if ( isEmpty())
            firstNode = lastNode = new ListNode<Integer>(insertItem);
        else 
            lastNode = lastNode.nextBasket = new ListNode<Integer>( insertItem );
    }

    public Integer removeFromBack() 
    {   
        Integer removedItem = (Integer) lastNode.topBucketInBasket;

        if ( firstNode == lastNode)
            firstNode = lastNode = null;
        else
        {
            ListNode<Integer> current = firstNode;

            while ( current.nextBasket != lastNode)
                current = current.nextBasket;

            lastNode = current;
            current.nextBasket = null;
        }

        return removedItem;
    }

    public boolean isEmpty()
    {
        return firstNode == null;
    }

    public List<Integer> merge(List<Integer> list1, List<Integer> list2)
    {
        List<Integer> modifiedList3 = new List<Integer>();

         Integer removedItemFromList1 = (Integer) list1.removeFromBack();
         Integer removedItemFromList2 = (Integer) list2.removeFromBack();

          ((Comparable) removedItemFromList1).compareTo( removedItemFromList2);

         int comparison = compareTo(removedItemFromList2);

         if ( comparison == 1)
            modifiedList3.insertAtBack(removedItemFromList2); 
            list1.insertAtBack(removedItemFromList1);

         if ( comparison == -1)
             modifiedList3.insertAtBack(removedItemFromList1);
            list2.insertAtBack(removedItemFromList2);

        return modifiedList3;   
    }
    @Override
    public int compareTo(Integer itemToCompare)
    {
        final int BEFORE = -1;
        final int AFTER = 1;
        if (this.removedItemFromList1 < list2.removedItemFromList2) return BEFORE;
        if (this.removedItemFromList1 > list2.removedItemFromList2) return AFTER;
    }

}

回答1:


You should implement the generic version Comparable<Integer> instead of the raw version Comparable.

Also, in the following

public class List<Integer> implements Comparable

That Integer is not the same as java.lang.Integer. Actually, it's something called formal type parameter in that context.

You probably want to change it to

//List is kind of ambiguous with java.util.List
public class MyList<E> implements Comparable<Integer> {

or may be something else instead of MyList.

If you implement Comparable it should be

@Override
public int compareTo(Object itemToCompare) {

and if Comparable<Integer> (which is the one you are after, I guess), it should be

@Override
public int compareTo(Integer itemToCompare)

The difference is the parameter types - Object and Integer.

It's kind of messy and not so clear what you are trying to acheive.

But the point that your List class implements Comparable indicates that you want to compare an instance of List with another instance. If that's the case then it should like something like this -

public class MyList<E> implements Comparable<MyList> {

    @Override
    public int compareTo(MyList other) {
        //compare and return result
    }

But the fact that the parameter type is Integer suggest that what you are actually trying to compare are Integers, in which case Integer is already Comparable.

((Comparable) removedItemFromList1).compareTo( removedItemFromList2);

You don't need to do all that

removedItemFromList1.compareTo(removedItemFromList2);

is enough. And if that's all you need then you don't need implement Comparable at all, so can remove that compareTo method also.




回答2:


The Comparable interface goes this way

public interface Comparable<T> {

  public int compareTo(T t){




  }

}

You should use the Type Parameter. Comparable is Type Parametrized.

So try it this way...

public class mList<Integer> implements Comparable<Integer>{


......



     public int compareTo(Integer itemToCompare){


        .....

    }



}


来源:https://stackoverflow.com/questions/12775772/how-do-i-correctly-implement-comparable-for-list-in-this-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!