Java 具有类型未预定义的变量的链表
我的链表使用了未预定义的变量,但我今天才学会使用这些变量。我已经设置了使用简单整数的列表,但是我意识到我不能以相同的方式比较变量,因为它们没有定义为整数,因此我在if语句上收到了一个语法错误,该语句试图比较链表节点的整数值,以查找列表中的最大值和最小值 我已将if语句标记为错误。如果需要更多的代码或信息,请告诉我Java 具有类型未预定义的变量的链表,java,singly-linked-list,Java,Singly Linked List,我的链表使用了未预定义的变量,但我今天才学会使用这些变量。我已经设置了使用简单整数的列表,但是我意识到我不能以相同的方式比较变量,因为它们没有定义为整数,因此我在if语句上收到了一个语法错误,该语句试图比较链表节点的整数值,以查找列表中的最大值和最小值 我已将if语句标记为错误。如果需要更多的代码或信息,请告诉我 package lab01Pkg; public class LinkedList <T> //<T> is defining the type of nod
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;
}
}
lab01Pkg包装;
公共类LinkedList//正在定义我们正在创建的节点的类型
{
私有整数大小;
专用节点头;
私有节点尾部;
公共链接列表()
{
尺寸=0;
head=null;
tail=null;
}
public void addToFront(T theData)//必须传入列表知道如何保存的变量类型。
{
节点tempNode=头;
head=新节点(数据、临时节点);
大小++;
}
公共void addLast(T theData)
{
if(isEmpty())
{
addToFront(数据);
}
其他的
{
节点tempNode=头;
while(tempNode.getLink()!=null)
{
tempNode=tempNode.getLink();
}
Node newNode=新节点(数据为空);
setLink(newNode);
大小++;
}
}
公营部门
{
如果(大小==0)
{
返回null;
}
节点tempNode=头;
T valueHold=tempNode.getData();
while(tempNode.getLink()!=null)
{
**if(tempNode.getData()valueHold)**//此处出错
{
valueHold=tempNode.getData();
}
tempNode=tempNode.getLink();
}
返回值保持;
}
公共void removeLast()
{
节点tempNode=头;
while(tempNode.getLink()!=null)
{
tempNode=tempNode.getLink();
}
tempNode=null;
}
public boolean isEmpty()//根据列表是否为空返回true或false。
{
返回大小==0;
}
公共字符串toString()
{
节点tempNode=头;
字符串theResult=“”;
while(tempNode!=null)
{
theResult=theResult+tempNode.getData().toString();
tempNode=tempNode.getLink();
}
返回结果;
}
}
要实现您想要的,您需要使用可比的
界面。阅读以了解可比
界面
public class LinkedList <T extends Comparable<T>> { ...
然后,您需要使类定义具有实现compariable
接口的泛型类型
public class LinkedList <T extends Comparable<T>> { ...
我刚刚概述了你需要做什么。希望你有这个想法。阅读更多有关
Comparable
与泛型的用法的阅读。要实现您想要的,您需要使用Comparable
界面。阅读以了解可比
界面
public class LinkedList <T extends Comparable<T>> { ...
然后,您需要使类定义具有实现compariable
接口的泛型类型
public class LinkedList <T extends Comparable<T>> { ...
我刚刚概述了你需要做什么。希望你有这个想法。有关
compariable
与泛型的用法的进一步阅读。要比较泛型类型,您需要强制执行它们以实现compariable,或者强制执行数据结构以接收要使用的比较方法。由于您使用的是泛型,列表无法确定类型。您需要使用Comparator和compareTo()方法来进行比较。请使用Integer包装类而不是简单的int类型。Integer实现可比较的接口。然后在min()函数中可以使用.compareTo方法。请查看此q/a以比较泛型类型您需要强制执行它们以实现可比较,或者强制执行数据结构以接收要使用的比较方法。由于您使用的是泛型,列表无法确定类型。您需要使用Comparator和compareTo()方法来进行比较。请使用Integer包装类而不是简单的int类型。Integer实现可比较的接口。然后在min()函数中可以使用.compareTo方法。看看这个问答