使用泛型用于对象数组的绑定不匹配错误

问题描述 投票:0回答:2

我之前使用过类似的界面,但是将它与通用对象一起使用,第二个对象给我带来了一些困难

这是我的驱动程序

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绑定不匹配类型的有界参数的有效替代:类型Part不是有界参数的有效替代在Prog2.main中的ListArray类型(Prog2.java:8)

从它的外观来看,这是一个问题,即如何在我的一个类中实现COmparable,而在另一个类中没有正确实现。我试着查看网站上的其他帖子并尝试实施它们无济于事。非常感谢!

java generics comparable
2个回答
1
投票

你已经指定你的ListArray只能扩展Comparable的类型

ListArray <E extends Comparable>

但是,你试图用Part对它进行参数化,而Comparable不会扩展Part

看起来你在制作Part通用时犯了一些错误。你应该有Comparable实施public class Part implements Comparable<Part> ,即:

compareTo

然后在Part中实现@Override public int compareTo(Part other) { // ... code here } 方法

Part

0
投票

你的问题源于你宣布E类使用扩展Comparable接口的通用ListArray这一事实。

同样适用于你的qazxsw poi类,你再次将它定义为接受扩展E界面的qazxsw poi。

当您尝试通过这样做创建新的Comparable时:

ListArray

它会有效地期望在界限内的东西,在这种情况下,这是实现ListArray<Part> partArray = new ListArray<Part>(13);界面的东西。由于你的Comparable对象没有这样做,这就是你得到这个错误的原因(编译器消息也是如此)。

如果你试图使用它们,我通常建议你对泛型有一个很好的阅读,因为你似乎缺乏理解它们。

© www.soinside.com 2019 - 2024. All rights reserved.