Java 打印任何类型的ArrayList元素的通用方法
出于练习目的,我试图编写一个通用方法,通过调用Java 打印任何类型的ArrayList元素的通用方法,java,arraylist,Java,Arraylist,出于练习目的,我试图编写一个通用方法,通过调用.toString()方法来显示ArrayList的元素。让我们假设.toString()做了我希望它做的事情 我在下面提出了这个解决方案,其中我的输入ArrayList类型为Object: public void printArralyList(ArrayList<Object> list){ for(Object o:list){ System.out.print(o.toString()); }
.toString()
方法来显示ArrayList
的元素。让我们假设.toString()
做了我希望它做的事情
我在下面提出了这个解决方案,其中我的输入ArrayList
类型为Object
:
public void printArralyList(ArrayList<Object> list){
for(Object o:list){
System.out.print(o.toString());
}
System.out.println();
}
public void打印阵列列表(阵列列表){
用于(对象o:列表){
系统输出打印(o.toString());
}
System.out.println();
}
然而,这是行不通的
printArralyList(new ArrayList<Integer>(Arrays.asList(1,2,3,5,8,13,21)));
printarylist(新的ArrayList(Arrays.asList(1,2,3,5,8,13,21));
我得到的编译错误是
The method printArralyList(ArrayList<Object>) is not applicable
for the arguments (ArrayList<Integer>
方法PrintArrayList(ArrayList)不适用
对于参数(ArrayList
如何解决这个问题?一个数组列表
不是数组列表
,即使整数
是一个对象
您需要在方法的参数中使用通配符,因为您不关心泛型类型参数是什么类型
public void printArralyList(ArrayList<?> list){
及
可以。您不需要泛型或通配符。您只需要一个简单的方法,它不为参数指定任何类型,这样您就可以随时传入ArrayList
public void printArralyList(ArrayList list){
for (Object o:list){
System.out.print(o.toString());
}
System.out.println();
}
这一错误的原因是由吉特曼在年解释的 但是,如果是出于实践目的,则应考虑多态性和。
publicstaticvoidprintiterable(Iterable-Iterable)
{
for(对象:iterable)
{
系统输出打印(对象);
}
System.out.println();
}
这可以与
ArrayList
参数一起使用,也可以与LinkedHashSet
一起使用,或者与实现Iterable
接口的任何其他东西一起使用……回答您的问题
声明ArrayList不等于ArrayList。即使从类型层次结构中的对象
传递了整数
需要记住的重要一点是,当您声明Collection myStrings
时,您会告诉编译器,只能为变量myString
分配在String
类型上创建的类的实例,这些实例是集合
Collection<Object> myObjects;
myObjects = new ArrayList<String>(); //Exception
myObjects = new HashSet<String>(); //Exception
myObjects = new HashSet<Object>(); //Valid
myObjects = new ArrayList<Object>(); //Valid
myObjects = new ArrayList<Integer>(); //Exception
收集myObjects;
myObjects=new ArrayList();//异常
myObjects=newhashset();//异常
myObjects=newhashset();//有效
myObjects=new ArrayList();//有效
myObjects=new ArrayList();//异常
为了解决类似的问题,java提供了一组允许您传递此消息的方法
有三种类型的意志卡支持各种不同的行为
-不变性/
关于它们的规则称为
当我们要使用列表元素时,要删除编译错误,应该使用协方差
Collection<? extends Object> myObjects;
myObjects = new ArrayList<String>(); //Valid
myObjects = new HashSet<String>(); //Valid
myObjects = new HashSet<Object>(); //Valid
myObjects = new ArrayList<Object>(); //Valid
myObjects = new ArrayList<Integer>(); //Valid
收集
,这就是吉特曼提出的答案
Java中的集合框架受支持。您应该熟悉它们,以充分受益于该框架
另一种解决方法是受益于以下通用方法:
public static <T> void printList<Iterable<T> iterable) {
for(T element : iterable){
System.out.printf("%s ",element);
}
System.out.println();
}
publicstaticvoidprintlist这将意味着使用原始类型,并销毁此类中的所有泛型类型检查。。。
Collection<Object> myObjects;
myObjects = new ArrayList<String>(); //Exception
myObjects = new HashSet<String>(); //Exception
myObjects = new HashSet<Object>(); //Valid
myObjects = new ArrayList<Object>(); //Valid
myObjects = new ArrayList<Integer>(); //Exception
<? extends T> - Covariance
<? super T> - Contravariance
<?> - Invariance/
Collection<? extends Object> myObjects;
myObjects = new ArrayList<String>(); //Valid
myObjects = new HashSet<String>(); //Valid
myObjects = new HashSet<Object>(); //Valid
myObjects = new ArrayList<Object>(); //Valid
myObjects = new ArrayList<Integer>(); //Valid
public static <T> void printList<Iterable<T> iterable) {
for(T element : iterable){
System.out.printf("%s ",element);
}
System.out.println();
}