简体   繁体   English

Java为什么不接受泛型中的我的LinkedList,而是接受它自己的泛型?

[英]How come Java doesn't accept my LinkedList in a Generic, but accepts its own?

For a class assignment, we can't use any of the languages bultin types, so I'm stuck with my own list. 对于班级分配,我们不能使用bultin类型的任何语言,因此我只能使用自己的列表。 Anyway, here's the situation: 无论如何,这是情况:

public class CrazyStructure <T extends Comparable<? super T>> {
    MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound
}

However: 然而:

public class CrazyStructure <T extends Comparable<? super T>> {
    LinkedList<MyTree<T>> trees;
}

Works. 作品。 MyTree impleements the Comparable interface, but MyLinkedList doesn't. MyTree实现Comparable接口,但MyLinkedList没有。 However, Java's LinkedList doesn't implement it either, according to this . 但是,根据this ,Java的LinkedList也不实现它。 So what's the problem and how do I fix it? 那么问题是什么,我该如何解决?

MyLinkedList: MyLinkedList:

public class MyLinkedList<T extends Comparable<? super T>> {
    private class Node<T> {
        private Node<T> next;
        private T data;

        protected Node();
        protected Node(final T value);
    }

    Node<T> firstNode;

    public MyLinkedList();
    public MyLinkedList(T value);

    //calls node1.value.compareTo(node2.value)
    private int compareElements(final Node<T> node1, final Node<T> node2);

    public void insert(T value);
    public void remove(T value);
}

MyTree: MyTree:

public class LeftistTree<T extends Comparable<? super T>>
        implements Comparable {

    private class Node<T> {
        private Node<T> left, right;
        private T data;
        private int dist;

        protected Node();
        protected Node(final T value);
    }

    private Node<T> root;

    public LeftistTree();
    public LeftistTree(final T value);
    public Node getRoot();

    //calls node1.value.compareTo(node2.value)
    private int compareElements(final Node node1, final Node node2);

    private Node<T> merge(Node node1, Node node2);
    public void insert(final T value);
    public T extractMin();
    public int compareTo(final Object param);
}

I assume your MyTree is the same as LeftistTree. 我假设您的MyTree与LeftistTree相同。 The problem with the signature is that it doesn't implement Comparable<LeftistTree<? super T>> 签名的问题在于它没有实现Comparable<LeftistTree<? super T>> Comparable<LeftistTree<? super T>> . Comparable<LeftistTree<? super T>>

So the signature should be: 因此签名应为:

public class LeftistTree<T extends Comparable<? super T>>
    implements Comparable<LeftistTree<? super T>>

The reason is that your MyLinkedList is not like a regular LinkedList. 原因是您的MyLinkedList与常规的LinkedList不同。 A regular LinkedList is of type: LinkedList<T> there are no bounds on T. You require with MyLinkedList that the parameter implement a Comparable of itself (or its superclass), but in fact LeftistTree was implementing a raw Comparable (or Comparable<?> ) so the Comparable was not guaranteed to be related to the type. 常规LinkedList的类型为: LinkedList<T>LinkedList<T>上没有界限。您需要使用MyLinkedList来使参数实现自身(或它的超类)的Comparable,但实际上LeftistTree在实现原始的Comparable(或Comparable<?> ),因此不保证Comparable与类型相关。

Why does your linked list must accept a Comparable typed? 为什么您的链表必须接受Comparable类型?

For a collection data structure, forcing your collection to only accept specific data type is very limiting. 对于集合数据结构,强制您的集合仅接受特定的数据类型是非常有限的。 If you would like to have a sorted linked list, it is better to accept any element and allow your linked list to accept a Comparator object. 如果您想要一个排序的链表,则最好接受任何元素,并允许链表接受Comparator对象。 If you do not provide a Comparator , then you can rely on the natural ordering of the contained element if they are of Comparable typed. 如果不提供Comparator ,则如果它们属于Comparable类型,则可以依靠所包含元素的自然顺序。

Take a look at the SortedSet or SortedMap api signature for some example. 看看一些示例的SortedSetSortedMap api签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实现我自己的LinkedList <LinkedList> Java中的数据结构? - How to implement my own LinkedList<LinkedList> data structure in Java? 如何在Java中的Stack / Queue类中实现我的通用LinkedList - How to implement my generic LinkedList in my Stack/Queue class in Java 在Java中创建我自己的LinkedList类的问题 - Issue creating my own LinkedList class in Java 基于java.util.linkedlist创建我自己的受限链表 - Create my own limited linkedlist based on java.util.linkedlist 用Java为自己的LinkedList类编写自己的peek()方法 - Writing my own peek() method for my own LinkedList class in Java 实现我自己的 LinkedList,向 Tail 中插入新元素无法正常工作 - Implementing my own LinkedList, insertion of a new element to the Tail doesn't work correctly 如何使位于自己文件中的私有节点 class 对我的卡组 class(LINKEDLIST)可见? - How to make a private node class located in its own file visible to my deck class (LINKEDLIST)? 为什么这个泛型不能识别它的超类边界(Java)? - Why doesn't this generic recognize its superclass boundary (Java)? 为什么LinkedList 在java 中没有initialCapacity? - why LinkedList doesn't have initialCapacity in java? “LinkedList不是通用的”错误Java - “LinkedList is not generic” error Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM