2

I am very confused about something in java. So the project I was given is write stacks in java, and the program begins with public class Stack<T extends Comparable<? super T>>. However, when trying to run it in my testing program, no matter the kind of Object I throw at it (Integer, String), it all return the error Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable. My question is how does this sort of generics that implement generic T but also extends Comparable work, and why String still returned the error message (I thought String already implemented Comparable<String>?). Thanks in advance for taking the time to read the question! Here is the rough outline of the code:

public class Stack <T extends Comparable<? super T>>{
  private T[] arr;
  private int ptr;
  private int size;

  public Stack(){
    this.size = 20;
    arr = (T[]) new Object[size];
    ptr = -1;
  }

  public boolean push(T element){
    try {
      arr[ptr+1] = element;
      ptr++;
      return true;
    } 
    catch (Exception e) {
      return false;
    }
  }
  public T[] toArray(){
    return arr;
  }
}

I got this error from creating a JUnit testing class, with the implementation of something like:

import static org.junit.Assert.assertEquals;
public class StackTester{
  @Test  
  public void testPush(){
    Stack<String> st = new Stack<String>();
    for (int i = 0; i < 5; i++){
        String var = new Integer(i).toString();
        st.push(var);
    }

    assertEquals(new Integer[]{"3","4","5"}, st.toArray());
  }
}
public class StackTester{
  @Test  
  public void testPush(){
    Stack<String> st = new Stack<String>();

    //normal case
    for (int i = 0; i < 5; i++){
        String var = new Integer(i).toString();
        st.push(var);
    }

    assertEquals(new Integer[]{3,4,5}, st.toArray());
  }
}

Also, I have not added a compareTo method as I don't know what it should compare to, and I don't think there's a particular use case for adding that method. I should add that the goal of this project is to use stacks to manipulate Strings (such as going from infix to postfix).

P.S: I would also mention that I don't really need to compare stacks in my project, which is why the stack class itself is not implementing the Comparable interface.

Je-Yu Chou
  • 21
  • 3
  • 2
    Please show a [mcve]. That error message looks like a runtime error, about an array of comparables. What are you doing in the stack class exactly? – Sweeper Feb 19 '23 at 07:38
  • I have added the necessary changes. Thank you so much for taking the time to answer! – Je-Yu Chou Feb 19 '23 at 10:36
  • First commandment of Java: Thou shalt not use arrays (unless thou hast not any choice in the matter). Ergo, replace `T[]` with `List` – Bohemian Feb 19 '23 at 10:50
  • Does this answer your question? [Array of Generics Can not Cast to Object to Comparable Java](https://stackoverflow.com/questions/65115400/array-of-generics-can-not-cast-to-object-to-comparable-java) – Progman Feb 19 '23 at 11:10
  • 1
    How is `new Integer[]{"3","4","5"}` supposed to work? – Holger Feb 20 '23 at 08:09

0 Answers0