在这种情况下,我如何正确实现List的Comparable? [英] How do I correctly implement Comparable for List in this instance?

查看:89
本文介绍了在这种情况下,我如何正确实现List的Comparable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是使用compareTo()方法比较removeItemFromList1和RemoveItemFromList2,然后将较小的那个值插入modifiedList3中,然后将两个RemovedFromList 1或2中的较大者放回其原始列表中.如果我有足够长的头发可以拉扯,那现在应该已经拉出来了...我不能正确铸造吗?我该如何正确地使用compareTo()方法完成此任务?

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;
    }

}

推荐答案

您应该实现通用版本Comparable<Integer>,而不是原始版本Comparable.

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

此外,在下面

public class List<Integer> implements Comparable

Integerjava.lang.Integer不同.实际上,在那种情况下,这就是所谓的形式类型参数.

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

您可能希望将其更改为

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

或者可以是MyList以外的其他东西.

or may be something else instead of MyList.

如果您实现Comparable,则应该

@Override
public int compareTo(Object itemToCompare) {

如果Comparable<Integer>(我想这是您想要的那个),应该是

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

@Override
public int compareTo(Integer itemToCompare)

区别在于参数类型-ObjectInteger.

The difference is the parameter types - Object and Integer.

有点混乱,不清楚要达到的目标.

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

但是List类实现Comparable的要点表明您想将List的一个实例与另一个实例进行比较.如果是这样,那么它应该喜欢这样的东西-

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
    }

但是参数类型为Integer的事实表明您实际上要比较的是Integer,在这种情况下Integer已经是Comparable.

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);

您不需要做所有的事情

removedItemFromList1.compareTo(removedItemFromList2);

就足够了.如果只需要这些,就根本不需要实现Comparable,因此也可以删除该compareTo方法.

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.

这篇关于在这种情况下,我如何正确实现List的Comparable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆