我以前使用过类似的接口,但是将其与通用对象一起使用时,第二个对象给我带来了一些麻烦
这是我的驱动程序
import java.io.*;
import java.util.*;
public class Prog2 {
public static void main (String[]args){
//Declare Variables
Scanner inFile = null;
ListArray<Part> partArray = new ListArray<Part>(13);
//Open the file
try {
inFile = new Scanner(new File("parts.txt"));
}
//If the file is not found, end the program
catch(FileNotFoundException e){
System.out.println("Error: File not found");
System.exit(0);
}
//While the file has new text, read it in
while(inFile.hasNext()){
//Read a line of code in
String temp = inFile.nextLine();
//split the line into an array
String[] tempA = temp.split(",[ ]*");
//place the specific info into variables
int pnum = Integer.parseInt(tempA[0]);
String name = tempA[1];
double price = Double.parseDouble(tempA[2]);
String warN = tempA[3];
int quant = Integer.parseInt(tempA[4]);
//add the info into an object
partArray.add(new Part(pnum, name,price,warN,quant));
}
}
}
该类旨在像数组列表一样编写
public class ListArray <E extends Comparable>{
//Declare Variables
private E[] list;
private int size;
//Construct Constructor
public ListArray(){
list = (E[]) new Comparable[10];
}
public ListArray(int capacity){
list = (E[]) new Comparable[capacity];
}
/*This method will allow users to get the variable stored
* at the index they specify
* @param: int index: the index of the wanted item
* @return: E: the item at the speicifed index */
public E get(int index){
return list[index];
}
/*This method will allow users to add an element to the
* end of the list array
* @param: E item: the item being added to the array */
public void add(E item){
list[size] = item;
size++;
}
/*This mehod will allow the user to find a specified item
* inside of the array
* @param: E target: the item the user wants to know the index of
* @return: int: the index of the item found */
public int find(E target){
for(int i = 0; i < size; i++){
if(target.compareTo(list[i]) == 0){
return i;
}
}
return -1;
}
/*This method will allow users to get the size of the array
* @return: int: the size of the array */
public int size(){
return size;
}
}
和从csv文件读取的Part类。
public class Part <E extends Comparable>{
//Declare Variables
private int pnum;
private String name;
private double price;
private String warh;
private int quant;
//Construct Constructor
public Part(){
pnum = 0;
name = "";
price = 0.0;
warh = "";
quant = 0;
}
public Part(int pnum, String name, double price, String warh, int quant){
this.pnum = pnum;
this.name = name;
this.price = price;
this.warh = warh;
this.quant = quant;
}
//Getters
public int getPnum(){
return pnum;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public String getWarh(){
return warh;
}
public int getQuant(){
return quant;
}
//Setters
public void setPnum(int pnum){
this.pnum = pnum;
}
public void setName(String name){
this.name = name;
}
public void setPrice(double price){
this.price = price;
}
public void setWarh(String warh){
this.warh = warh;
}
public void setQuant(int quant){
this.quant = quant;
}
运行程序时,在控制台内部收到此错误
线程“ main”中的异常java.lang.Error:未解决的编译问题: 绑定不匹配:Part类型不是ListArray类型的bounded参数的有效替代品 绑定不匹配:Part类型不是ListArray类型的bounded参数的有效替代品 在Prog2.main(Prog2.java:8)
从它的外观来看,这是一个问题,即如何在我的一个类中实现COmparable,而在另一个类中实现不正确。我尝试查看网站上的其他帖子,并尝试将其实施无济于事。非常感谢!
答案 0 :(得分:1)
您已将ListArray
指定为只能扩展为Comparable
的类型进行参数化
ListArray <E extends Comparable>
但是,您正在尝试使用Part
对其进行参数化,该参数不会扩展Comparable
。
在使Part
通用时,您似乎犯了一些错误。您应该Part
实现Comparable
,即:
public class Part implements Comparable<Part>
然后在Part
中实现compareTo
方法
@Override
public int compareTo(Part other) {
// ... code here
}
答案 1 :(得分:0)
这里的问题源于您声明Part
类使用泛型E
扩展了Comparable
接口的事实。
ListArray
类也是如此,在该类中,您再次将其定义为接受扩展E
接口的Comparable
。
当您尝试通过以下方式创建新的ListArray
时:
ListArray<Part> partArray = new ListArray<Part>(13);
它会有效地期望某些范围内的东西,在这种情况下,这是实现Comparable
接口的东西。由于您的Part
对象没有这样做,所以这是您收到此错误的原因(而且编译器消息对此很有帮助)。
如果您尝试使用泛型,我通常会建议您读一读,因为您似乎缺乏对它们的理解。