创建具有多种对象类型的 ArrayList? [英] Create an ArrayList with multiple object types?
本文介绍了创建具有多种对象类型的 ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建具有整数和字符串输入类型的 ArrayList
?如果我创建一个:
How do I create an ArrayList
with integer and string input types? If I create one as:
List<Integer> sections = new ArrayList <Integer>();
这将是一个 Integer
类型 ArrayList
.如果我创建一个:
that will be an Integer
type ArrayList
.
If I create one as:
List<String> sections = new ArrayList <String>();
那将是 String
类型.我如何创建一个 ArrayList
,它可以接受整数和字符串输入类型?谢谢.
that will be of String
type.
How can I create an ArrayList
which can take both integer and string input types?
Thank you.
推荐答案
你可以这样:
List<Object> sections = new ArrayList <Object>();
(推荐)另一种可能的解决方案是创建一个具有两个参数的自定义模型类,一个是整数,另一个是字符串.然后使用该对象的 ArrayList
.
(Recommended) Another possible solution would be to make a custom model class with two parameters one Integer and other String. Then using an ArrayList
of that object.
这篇关于创建具有多种对象类型的 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文