Java Syntax for a list of comparable objects

三世轮回 提交于 2019-12-22 05:20:49

问题


I'm writing a method that takes as its only parameter a list of comparable objects and doesn't return anything. I am uncertain as to the syntax that it should have:

public static void methodName(List<Comparable<Object>> list) {
    // Do some stuff
}

I think this is wrong because of the <Object> as a type for Comparable, which would mean the list can take an Integer and a Boolean as objects, but I don't want that. I want the list to take only one type, but that type has to implement the Comparable interface. How do I achieve that?


回答1:


Maybe make it generic?

public static <E extends Comparable<E>> void methodName(List<E> list) ...


来源:https://stackoverflow.com/questions/10763390/java-syntax-for-a-list-of-comparable-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!