【问题标题】:Linked List with Variables whose type are not predefined带有未预定义类型的变量的链表
【发布时间】:2018-09-14 21:59:25
【问题描述】:

我的链接列表使用未预定义的变量,但我今天才学会使用这些。我已设置列表以使用简单整数,但我意识到我无法以相同的方式比较我的变量,因为它们没有定义为整数,因此我在尝试比较整数值的 if 语句中收到语法错误从链表节点中找出链表中最大和最小的节点。

我已将 if 语句标记为错误。请告诉我是否需要更多代码或信息。

package lab01Pkg;

public class LinkedList <T> //<T> is defining the type of node we are creating
{
    private int size;
    private Node<T> head;
    private Node<T> tail;

    public LinkedList()
    {
        size = 0;
        head = null;
        tail = null;
    }

    public void addToFront( T theData ) //Must pass in the type of variable the list knows how to hold.
    {
        Node<T> tempNode = head;
        head = new Node<T>( theData, tempNode );
        size++;
    }

    public void addLast( T theData )
    {
        if ( isEmpty() )
        {
            addToFront(theData);
        }
        else
        {
            Node<T> tempNode = head;
            while ( tempNode.getLink() != null )
            {
                tempNode = tempNode.getLink();
            }
            Node<T> newNode = new Node<T>( theData, null );
            tempNode.setLink(newNode);
            size++;
        }
    }

    public T min()
    {
        if (size == 0)
        {
            return null;
        }
        Node<T> tempNode = head;
        T valueHold = tempNode.getData();
        while ( tempNode.getLink() != null )
        {
            **if (tempNode.getData() < valueHold)** //Error Here
            {
                valueHold = tempNode.getData();
            }
            tempNode = tempNode.getLink();
        }
        return valueHold;
    }

    public T max()
    {
        if (size == 0)
        {
            return null;
        }
        Node<T> tempNode = head;
        T valueHold = tempNode.getData();
        while ( tempNode.getLink() != null )
        {
            **if (tempNode.getData() > valueHold)** //Error here
            {
                valueHold = tempNode.getData();
            }
            tempNode = tempNode.getLink();
        }
        return valueHold;
    }

    public void removeLast()
    {
        Node<T> tempNode = head;
        while (tempNode.getLink() != null)
        {
            tempNode = tempNode.getLink();
        }
        tempNode = null;
    }

    public boolean isEmpty() //Returns true or false based on if list is empty or not.
    {
        return size == 0;
    }

    public String toString()
    {
        Node<T> tempNode = head;
        String theResult = "";
        while ( tempNode != null)
        {
            theResult = theResult + tempNode.getData().toString();
            tempNode = tempNode.getLink();
        }
        return theResult;
    }

}

【问题讨论】:

  • 要比较泛型类型,您需要强制它们实现可比较或强制数据结构接收要使用的比较方法。
  • 由于您使用的是泛型,因此 List 无法确定类型。您将需要使用 Comparator 并使用 compareTo() 方法进行比较。
  • 使用 Integer 包装类而不是简单的 int 类型。 Integer 实现了可比较的接口。然后在您的 min() 函数中,您可以使用 .compareTo 方法。看看这个 q/a stackoverflow.com/questions/21544716/…

标签: java singly-linked-list


【解决方案1】:

要实现你想要的,你需要使用Comparable接口。阅读this article 了解Comparable 界面。

然后你需要让你的类定义有一个实现Comparable接口的泛型类型。

public class LinkedList <T extends Comparable<T>> { ... 

并且您需要确保您计划添加到此列表中的对象正在实现Comparable 接口。通常像 Integer、Long 这样的 java 内置类已经实现了这一点。如果您使用自定义对象,请确保在这些类中实现该接口。

然后,在 if 条件中使用 compareTo 方法,而不是 &lt;&gt; 标记。例如:

if (tempNode.getData().compareTo(valueHold) > 0) // tempNode is larger

我刚刚概述了您需要做的事情。希望你明白了。阅读 this 以进一步了解 Comparable 与泛型的用法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2014-05-17
    • 2011-06-03
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多