用于可比较对象列表的Java语法 [英] Java Syntax for a list of comparable objects

查看:137
本文介绍了用于可比较对象列表的Java语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个方法,它将唯一参数作为可比较对象的列表,并且不返回任何内容。我不确定它应具有的语法:

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
}

我认为这是错误的因为< Object> 作为Comparable的类型,这意味着列表可以将整数和布尔值作为对象,但我不希望这样。我希望列表只采用一种类型,但该类型必须实现Comparable接口。我如何实现这一目标?

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?

推荐答案

也许让它变得通用?

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

这篇关于用于可比较对象列表的Java语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆