Bound mismatch error using Generics for an object array

3551 views java
2

I have used the comparable interface before but using it with generic objects and a second object has been causing me some difficulties

Here is my driver program

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));                   
    }



    }

}

The class meant to be written like an Array list

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;
    }
}

and the Part class that reads in from a csv file.

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;
        }

When I run the program, I am given this error inside of the console

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Bound mismatch: The type Part is not a valid substitute for the bounded parameter of the type ListArray Bound mismatch: The type Part is not a valid substitute for the bounded parameter of the type ListArray at Prog2.main(Prog2.java:8)

From the looks of it, this is a problem of how COmparable is implemented in one of my classes, and it not being correctly implemented in the other. I tried looking at other posts on the website and tried implementing them

answered question

Don't use raw types. So basically do ListArray <E extends Comparable<E>> and Part <E extends Comparable<E>> to avoid them

I'm not experienced with generics but with Comparable they should be used in this way <E extends Comparable<E>>

1 Answer

12

Your problem here stems from the fact that you declared the Part class of using a generic E which extends the Comparable interface.

Same goes for your ListArray class, in which you define it again as accepting a E which extends the Comparable interface.

When you try to create a new ListArray by doing so:

ListArray<Part> partArray = new ListArray<Part>(13);

it'll effectively expect something that is within bounds, in this case this being something that implements the Comparable interface. Since your Part object does not do so, this is reason why you get this error (also the compiler message is quite informative about this).

I would generally suggest you have a good read on generics if you attempt to use them, as it seems that you're lacking in understanding them.

posted this

Have an answer?

JD

Please login first before posting an answer.