Java 为什么我会得到一个';未找到源';调用以接口作为构造函数参数的类时出错?
我正在尝试使用接口创建一些字符串排序方法。应用程序驱动程序类调用主SimplePriorityQueue对象类,但在进行此调用时,它会传入一个实现接口的类作为参数。有几个类实现了接口(一个按长度比较,一个按字母顺序(按值),一个按反向长度,等等),这就是为什么我被告知SimplePriorityQueue类中的一个构造函数应该能够处理所有这些类 使用Eclipse调试器时,只要应用程序类执行行pq=new SimplePriorityQueue(new StringByLength()),就会弹出“source not found”错误;如代码注释所示 以下是我所拥有的:Java 为什么我会得到一个';未找到源';调用以接口作为构造函数参数的类时出错?,java,eclipse,debugging,interface,constructor,Java,Eclipse,Debugging,Interface,Constructor,我正在尝试使用接口创建一些字符串排序方法。应用程序驱动程序类调用主SimplePriorityQueue对象类,但在进行此调用时,它会传入一个实现接口的类作为参数。有几个类实现了接口(一个按长度比较,一个按字母顺序(按值),一个按反向长度,等等),这就是为什么我被告知SimplePriorityQueue类中的一个构造函数应该能够处理所有这些类 使用Eclipse调试器时,只要应用程序类执行行pq=new SimplePriorityQueue(new StringByLength()),就会弹
这是一个排序类。还有几个,但几乎都是这样
//StringByLength class, which sorts by length
package example.cs151xx;
import java.util.Comparator;
public class StringByLength implements Comparator {
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
return s2.length() - s1.length();
}
public boolean equals(Object o)
{return (o instanceof StringByLength);}
}
那么,我在SimplePriorityQueue类的构造函数中做错了什么?提前感谢您的帮助。您实现了
java.util.Comparator
而不是cs15200.review.srijitag.Comparator
,其中java.util.Comparator
在调试时找不到源代码。在阅读了第步中的线程后,我尝试尽可能地返回,然后再次进入,它工作了。事实上,仅仅运行应用程序(而不是调试)也可以毫无问题地运行。但我还是想知道为什么会出现源未找到错误
//StringByLength class, which sorts by length
package example.cs151xx;
import java.util.Comparator;
public class StringByLength implements Comparator {
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
return s2.length() - s1.length();
}
public boolean equals(Object o)
{return (o instanceof StringByLength);}
}
//the relevant portion of the Application class
import example.cs151xx.Prompt;
import cs15200.review.srijitag.SimplePriorityQueue;
import example.cs151xx.StringByLength;
import cs15200.review.srijitag.StringByLengthReverse;
import cs15200.review.srijitag.StringByValue;
import cs15200.review.srijitag.StringByValueIgnoreCase;
import cs15200.review.srijitag.IntegerIncreasing;
import java.util.Comparator;
public class Application {
////////////////
//Driver Program
////////////////
public static void main(String[] args)
{
//Construct object to test; let the user specify what Comparator
// object to construct to order the priority queue
SimplePriorityQueue pq;
char howPrioritize = Prompt.forChar("Enter how to prioritize queue\n v (by value)\n i (value ignore case)\n l (by length)\n r (by reverse length)\n\nChoice","virl");
if (howPrioritize == 'v')
pq = new SimplePriorityQueue(new StringByValue());
else if (howPrioritize == 'i')
pq = new SimplePriorityQueue(new StringByValueIgnoreCase());
else if (howPrioritize == 'l')
pq = new SimplePriorityQueue(new StringByLength()); //ERROR THROWN
else
pq = new SimplePriorityQueue(new StringByLengthReverse());
[...more code to finish up rest of driver class]
//The relevant parts SimplePriorityQueue class which has the constructors, accessors, and methods
package cs15200.review.srijitag;
import example.cs151xx.StringByLength;
import java.util.Comparator;
public class SimplePriorityQueue{
public SimplePriorityQueue(Comparator whatever){
check = whatever;
}
public void enqueue(Object item){
if(list.length==rear+1)
doubleSize();
if (rear==-1)
list[0]=item;
else{
for(int i=rear;i>=0;i--){
int z = check.compare(item,list[i]);
if(z<=0){
list[i+1]=list[i];
list[i]=item; }
else{
list[i+1]=item;
}
}
}
rear++;
}
[...more accessors and mutators]
//instance variables
private Object[] list=new Object[1];
private int rear=-1;
private Comparator check;
}
package cs15200.review.srijitag;
import example.cs151xx.StringByLength;
import java.util.Comparator;
public class temp {
public static void main(String[] args) {
Comparator z = new StringByLength();
System.out.println( z.compare("hi", "what's up"));
Comparator y = new StringByValue();
System.out.println(y.compare("hi", "what's up"));
System.out.println( lookee(new StringByLength())); //should return the same result as z.compare(...)
System.out.println(lookee(new StringByValue())); //should return the same result as y.compare(...)
}
public static int lookee(Comparator test){
return test.compare("hi", "what's up");
}
}