Generics in Java, Merge method

后端 未结 1 399
北荒
北荒 2021-01-25 23:18

I have to create a Merge method to merge together two lists (Array-Based Lists). My method works, but now I have to change my method to generics. This is my method without gener

相关标签:
1条回答
  • 2021-01-25 23:52

    Assuming you have declared <T extends Comparable<T> > at the class level, remove it on the method:

    class OrderedArrayList1<T extends Comparable<T> > {
      public OrderedArrayList1<T> merge(OrderedArrayList1<T> list2) {
        ...
      }
    }
    

    Otherwise, you're defining a different type variable which just happens to have the same name, hence the slightly cryptic "T cannot be converted to T" message.

    0 讨论(0)
提交回复
热议问题