Java 包含整数和字符串的数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18033287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Arraylist containing Integers and Strings
提问by user2648572
I want to create a Arraylist which should contain Integers and Strings.. Is that possible?
我想创建一个包含整数和字符串的 Arraylist .. 这可能吗?
I have created two Arraylist as given below:
我创建了两个 Arraylist,如下所示:
ArrayList<Integer> intList=new ArrayList<Integer>();
intList.add(1);
intList.add(2);
ArrayList<String> strList=new ArrayList<String>();
strList.add("India");
strList.add("USA");
strList.add("Canada");
I want to put intList & strList into a new ArrayList.
我想将 intList & strList 放入一个新的 ArrayList 中。
Can I do that?? If so, How??
我可以这样做吗??如果是这样,如何?
采纳答案by Ravi Thapliyal
You can do this as follows but have to give up on generics for the list container.
您可以按如下方式执行此操作,但必须放弃列表容器的泛型。
List<List> listOfMixedTypes = new ArrayList<List>();
ArrayList<String> listOfStrings = new ArrayList<String>();
ArrayList<Integer> listOfIntegers = new ArrayList<Integer>();
listOfMixedTypes.add(listOfStrings);
listOfMixedTypes.add(listOfIntegers);
But, a better way would be to use a Map
to keep track of the two lists since the compiler would no longer be able to prevent you from mixing types like putting a String into an Integer list.
但是,更好的方法是使用 aMap
来跟踪两个列表,因为编译器将不再能够阻止您混合类型,例如将 String 放入 Integer 列表中。
Map<String, List> mapOfLists = new HashMap<String, List>();
mapOfLists.put("strings", listOfStrings);
mapOfLists.put("integers", listOfIntegers);
mapOfLists.get("strings").add("value");
mapOfLists.get("integers").add(new Integer(10));
回答by Suresh Atta
If it's avoidable, please avoid this list of Object type. Go for individual lists.
如果可以避免,请避免此对象类型列表。去个人名单。
If not then you should go for type of Object
如果没有,那么你应该去寻找类型 Object
List<Object> list = new ArrayList<Object>();
which accept all the type Objects, but have to take care while retrieving.
它接受所有类型的对象,但在检索时必须小心。
Checking the objects while retrieving
检索时检查对象
for (Object obj: list) {
if (obj instanceof String){
// this is string
} else if (obj instanceof Integer) {
// this is Integer
}
}
回答by ihsan kocak
List<Object> oList=new ArrayList<Object>();
回答by MAnyKey
You can use tagged sum types: Either<A, B>
is either Left<A, B>
or Right<A, B>
. In Java it will look like:
您可以使用标记的总和类型:Either<A, B>
是Left<A, B>
或Right<A, B>
。在 Java 中,它看起来像:
public interface Either<A, B>;
public class Left<A, B> implements Either<A, B> {
public final A value;
public Left(A value) {
this.value = value;
}
}
public class Right<A, B> implements Either<A, B> {
public final B value;
public Right(B value) {
this.value = value;
}
}
So, you can use ArrayList<Either<Integer, String>>
.
因此,您可以使用ArrayList<Either<Integer, String>>
.
for (Either<Integer, String> either : intsOrStrings) {
if (either instanceof Left) {
Integer i = ((Left<Integer, String>) either).value;
} else if (either instanceof Right) {
String s = ((Right<Integer, String>) either).value;
}
}
This approach is more type-safe than using Object
.
这种方法比使用Object
.
回答by Aman Kumar
I am providing you that example which is implement in my project inquiryhere.com Click here to Visit
我正在为您提供在我的项目中实施的示例 queryhere.com单击此处访问
java code...
代码...
public class hash_Map {
public HashMap<Integer, String> getQuestionTagWithId(int qId) throws SQLException{
DatabaseConnection dc = new DatabaseConnection();
HashMap<Integer, String> map = new HashMap<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
con = dc.getConnection();
String sql = "select tag_id as unique_id,(select topic_name from topic where unique_id = question_topic_tag.tag_id)topic_name from question_topic_tag where question_id =?";
ps = con.prepareStatement(sql);
ps.setInt(1, qId);
rs = ps.executeQuery();
while(rs.next()){
int questionTagId = rs.getInt("unique_id");
String questionTag = rs.getString("topic_name");
map.put(questionTagId, questionTag);
}
}catch(SQLException msg){
throw msg;
}finally{
if(rs != null){
try{
rs.close();
}catch(SQLException msg){
}
}
if(ps != null){
try{
ps.close();
}catch(SQLException msg){
}
}
if(con != null){
try{
con.close();
}catch(SQLException msg){
}
}
}
return map;
}}
Jspbean
豆豆
<jsp:useBean class="com.answer.hash_Map" id="SEO" scope="page" />
jstl code, I am working with jstl
jstl 代码,我正在使用 jstl
<c:forEach items="${SEO.getQuestionTagWithId(param.Id)}" var="tag" varStatus="loop">
${tag.key}${tag.value}
</c:forEach>