0

I got the following code:

public static <T extends Comparable<T>> void isPalindrome(T[] array)
{
 //body
}

and also these arrays:

Integer[] intArray = {1,2,5,7,9,0,7}; 
Integer[] intArray2 = {1,1,2,2,2,2,1,1}; 
String[] strArray = {"a","b","c","c","b","a"}; 
String[] strArray2 = {"a","b","c","e","f","a"}; 
isPalindrome(intArray);
isPalindrome(intArray2);
isPalindrome(strArray); 
isPalindrome(strArray2);

before I compile and when I hover over ispalindrome it shows me the following error:

 method isPalindrome in class Main cannot be applied to given types;
 required: T[]
 found: String[]
 reason: inference variable T has incompatible bounds
 upper bounds: Comparable<T>
 lower bounds: String
 where T is a type-variable:
 T extends Comparable<T> declared in method <T>isPalindrome(T[])

I tried to use <T extends Comparable<? super T>> but it didn't work.

Edit:

I tried but nothing works for me

I have also Comparable.java class

public interface Comparable<T> {
 
 
 int compareTo(T other);
}
asked Apr 21, 2021 at 20:33
5
  • 3
    Works fine here. Commented Apr 21, 2021 at 20:39
  • Unable to reproduce. Try a clean rebuild. Commented Apr 21, 2021 at 20:45
  • The code seems good and It works fine. Commented Apr 21, 2021 at 20:55
  • Show the fully-qualified name of Comparable in your isPalindrome signature. Commented Apr 21, 2021 at 21:33
  • Why do you compare with compareTo() instead of equals()? Commented Apr 22, 2021 at 1:13

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.