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

发布于 2024-09-01 08:12:33 字数 1963 浏览 4 评论 0 原文

对于课堂作业,我们不能使用任何类型的语言,因此我只能使用自己的列表。不管怎样,情况是这样的:

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

但是:

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

有效。 MyTree 实现了 Comparable 接口,但 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);
}

我的树:

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

For a class assignment, we can't use any of the languages bultin types, so I'm stuck with my own list. 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. However, Java's LinkedList doesn't implement it either, according to this. So what's the problem and how do I fix it?

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佼人 2024-09-08 08:12:34

我假设你的 MyTree 与 LeftistTree 相同。签名的问题在于它没有实现Comparable>。

所以签名应该是:

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

原因是你的MyLinkedList不像常规的LinkedList。常规 LinkedList 的类型为:LinkedList T 上没有界限。您要求 MyLinkedList 的参数实现其自身(或其超类)的 Comparable,但实际上 LeftistTree 正在实现一个raw Comparable(或 Comparable),因此不能保证 Comparable 与类型相关。

I assume your MyTree is the same as LeftistTree. The problem with the signature is that it doesn't implement 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. 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.

战皆罪 2024-09-08 08:12:34

为什么您的链接列表必须接受 Comparable 类型?

对于集合数据结构,强制集合仅接受特定数据类型是非常有限的。
如果您想要一个排序的链表,最好接受任何元素并允许您的链表接受 Comparator 对象。如果您不提供Comparator,那么您可以依赖所包含元素的自然排序(如果它们是Comparable 类型)。

看一下 SortedSet< /a> 或 SortedMap api 签名一些例子。

Why does your linked list must accept a Comparable typed?

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. 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.

Take a look at the SortedSet or SortedMap api signature for some example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文