SauerTrout
SauerTrout

Reputation: 499

Passing Multiple Generics Into Java Method

Using Java. The goal is to search for a value, given as a generic, in an ArrayList, also given as a generic.

My Student class (pertinent parts)

public class Student<T> implements Comparable
{
    private String studName;
    private Integer gradeAverage;

    public Student(String nameIn, int gradeIn)
    {
        studName = nameIn;
        gradeAverage = gradeIn;
    }

    public int compareTo(Object obj)
    {
        Student s1 = (Student)obj;
        return(this.gradeAverage - s1.gradeAverage);
    }
}

My Search; thinking there may be a problem with the generic specifications

public class SearchMethods<T,S>
{
    public <T extends Comparable, S extends Comparable> void BinarySearch(T[] inputArray, S searchValue)
    {
        boolean found = false;
        for(int i = 0; i < inputArray.length; i++)
        {
            T search = inputArray[i];
            if(searchValue.compareTo(search) == 0)
            {
                System.out.println(searchValue + " is at index " + i);
                found = true;
            }
        }
        if(found == false)
        {
            System.out.println(searchValue + " was not found");
        }
    }    
}

And my main()

public static void main(String[] args) 
{
    Student studentOne = new Student("James",92);
    Student studentTwo = new Student("Mary",95);
    Student studentThree = new Student("Bobbie",82);
    Student studentFour = new Student("Emily",100);
    Student studentFive = new Student("Joey",88);

    ArrayList<Student> studentList = new ArrayList<Student>();
    studentList.add(studentOne);
    studentList.add(studentTwo);
    studentList.add(studentThree);
    studentList.add(studentFour);
    studentList.add(studentFive);

    SearchMethods<ArrayList, Student> searchMethods = new SearchMethods<ArrayList, Student>();

    searchMethods.BinarySearch(studentList, studentOne);  //Should print that it was found at index 0

The given compiler error states that an argument mismatch, that ArrayList cannot be converted to T#1[]. But that's the whole point of generics, right?? Interestingly, no analogous error is given for the second type, but maybe the compiler just hasn't read ahead that far.

I'm pretty sure my syntax is OK at the class level, so the error is likely with the calling objects in main(). Though, I could be wrong.

Thanks in advance!

Upvotes: 0

Views: 623

Answers (1)

user3773246
user3773246

Reputation:

You need to convert arraylist to an array. Check the argument for Binary Search.

Try this:

  SearchMethods<ArrayList, Student> searchMethods = new SearchMethods<ArrayList, Student>();

    searchMethods.BinarySearch(studentList.toArray(new Student[studentList.size()]), studentOne);

You could also change BinarySearch where you work with an arraylist.

While this is not part of the question, it is important not compute the difference for compareTo or you will get overflow error.

Try this:

 class Student<T> implements Comparable
{
private String studName;
private Integer gradeAverage;

public Student(String nameIn, int gradeIn)
{
    studName = nameIn;
    gradeAverage = gradeIn;
}

public int compareTo(Object obj)
{
    Student s1 = (Student)obj;
    if (this.gradeAverage < s1.gradeAverage){
        return -1;
    }
    if(this.gradeAverage == s1.gradeAverage){
        return 0;
    }

    return 1;
}

@Override
public String toString(){
    return "student name="+studName +" grade average= " + gradeAverage;
}
}

Upvotes: 1

Related Questions