Java绑定不匹配错误-LinkedList
好的,这是我正在使用的界面Java绑定不匹配错误-LinkedList,java,Java,好的,这是我正在使用的界面 /* instance of classes that realize this interface can be compared */ public interface Comparable<E> { /* Method to compare this object to the argument object * @param obj - the argument object * @return - returns a negative
/* instance of classes that realize this interface can be compared */
public interface Comparable<E>
{
/* Method to compare this object to the argument object
* @param obj - the argument object
* @return - returns a negative integer if this object < obj
*/
int compareTo(E obj);
可以比较实现此接口的类的实例*/
公共界面可比
{
/*方法将此对象与参数对象进行比较
*@param obj-参数对象
*@return-如果此对象import java.util.*;
/* a class to represent an ordered List. the data is stored
* in a linked list data field
*/
public class OrderedList<E extends Comparable<E>>
implements Iterable<E>
{
/* a linked list to contain data */
private LinkedList<E> theList = new LinkedList<E>();
/* Insert Obj into the list preserving the lists order
* @param pre - the items in the list ordered
* @param post - obj has been inserted into the list such
* that the items are still in order
*/
public void add(E obj)
{
ListIterator<E> iter = theList.listIterator();
// find the insertion position and insert
while(iter.hasNext())
{
if(obj.compareTo(iter.next()) < 0)
{
// Iterator has stepped over the first element that
// is greater than the element to be inserted
// move the iterator back one
iter.previous();
// insert the element
iter.add(obj);
//exit the loop and return
return;
}
}
/* assert - all items were examined and no item is larger than
* the element to be inserted
* add the new item to the end of the list
*/
iter.add(obj);
}
/* returns the element at the specified position */
public E get(int index)
{
return theList.get(index);
}
/* returns an iterator to this ordered List */
public Iterator<E> iterator()
{
return theList.iterator();
}
/* returns the size of the list */
public int size()
{
return theList.size();
}
}
import java.util.*;
/*表示有序列表的类。数据被存储
*在链表数据字段中
*/
公共类OrderedList
可移植的
{
/*包含数据的链表*/
私有LinkedList theList=新LinkedList();
/*将Obj插入列表,保留列表顺序
*@param pre-列表中的项目已排序
*@param post-obj已插入此类列表中
*这些物品仍在整理中
*/
公共无效添加(E obj)
{
ListIterator iter=theList.ListIterator();
//找到插入位置并插入
while(iter.hasNext())
{
if(对象比较(iter.next())<0)
{
//迭代器已跨过
//大于要插入的元素
//将迭代器向后移动一步
iter.previous();
//插入元素
iter.add(obj);
//退出循环并返回
返回;
}
}
/*断言-检查了所有项目,没有项目大于
*要插入的元素
*将新项目添加到列表的末尾
*/
iter.add(obj);
}
/*返回指定位置的元素*/
公共E-get(int索引)
{
返回list.get(索引);
}
/*返回此有序列表的迭代器*/
公共迭代器迭代器()
{
返回list.iterator();
}
/*返回列表的大小*/
公共整数大小()
{
返回list.size();
}
}
以及问题所在的测试类
import java.util.*;
public class TestOrderedList
{
/* Traverses ordered list and displays each element
* displays and error message if an element is out of order
* @param testList - an ordered list of integers
*/
public static void traverseAndShow(OrderedList<Integer> testList)
{
int prevItem = testList.get(0);
/* traverse ordered list and display any value that
* is out of order
*/
for(int thisItem : testList)
{
System.out.println(thisItem);
if(prevItem > thisItem)
System.out.println("***Failed, value is " + thisItem);
prevItem = thisItem;
}
}
public static void main(String[] args)
{
OrderedList<Integer> testList = new OrderedList<Integer>();
final int MAX_INT = 500;
final int START_SIZE = 100;
// create a random generator
Random random = new Random();
for(int i = 0; i < START_SIZE; i++)
{
int anInteger = random.nextInt(MAX_INT);
testList.add(anInteger);
}
//Add to beginning and end of list.
testList.add(-1);
testList.add(MAX_INT + 1);
traverseAndShow(testList); // traverse and display
}
}
import java.util.*;
公共类TestOrderedList
{
/*遍历有序列表并显示每个元素
*如果元素出现故障,则显示错误消息
*@param testList-整数的有序列表
*/
公共静态无效遍历显示(OrderedList testList)
{
int previitem=testList.get(0);
/*遍历有序列表并显示
*它坏了
*/
for(int thisItem:testList)
{
系统输出打印项次(本项);
如果(PreviItem>thisItem)
System.out.println(“***失败,值为”+此项);
PreviItem=此项;
}
}
公共静态void main(字符串[]args)
{
OrderedList testList=新OrderedList();
最终整数MAX_int=500;
最终int START_大小=100;
//创建一个随机生成器
随机=新随机();
对于(int i=0;i
当我尝试在测试类中使用Integer时,会出现绑定不匹配错误。问题出在哪里?您已经定义了自己的
可比
接口,但整数
没有实现。从生成路径中删除自定义接口,以便可以使用内置接口。问题是,Integer
实现了java.lang.Comparable
,而您有自己的Comparable
接口,这是不同的
您的可比
界面似乎是多余的,尽管您可以删除它并使用内置界面