Java 如何找出每个对象在 ArrayList<Object> 中的类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/106336/
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
How do I find out what type each object is in a ArrayList<Object>?
提问by WolfmanDragon
I have a ArrayList made up of different elements imported from a db, made up of strings, numbers, doubles and ints. Is there a way to use a reflection type technique to find out what each type of data each element holds?
我有一个由从数据库导入的不同元素组成的 ArrayList,由字符串、数字、双精度数和整数组成。有没有办法使用反射类型技术来找出每个元素拥有的每种类型的数据?
FYI: The reason that there is so many types of data is that this is a piece of java code being written to be implemented with different DB's.
仅供参考:有这么多类型的数据的原因是这是一段 Java 代码,正在编写以使用不同的数据库实现。
采纳答案by Frank Krueger
In C#:
Fixed with recommendation from Mike
在 C# 中:
根据Mike 的建议修复
ArrayList list = ...;
// List<object> list = ...;
foreach (object o in list) {
if (o is int) {
HandleInt((int)o);
}
else if (o is string) {
HandleString((string)o);
}
...
}
In Java:
在 Java 中:
ArrayList<Object> list = ...;
for (Object o : list) {
if (o instanceof Integer)) {
handleInt((Integer o).intValue());
}
else if (o instanceof String)) {
handleString((String)o);
}
...
}
回答by skiphoppy
Just call .getClass()
on each Object
in a loop.
只需在循环中调用.getClass()
每个Object
。
Unfortunately, Java doesn't have map()
. :)
不幸的是,Java 没有map()
. :)
回答by Fabian Steeg
for (Object object : list) {
System.out.println(object.getClass().getName());
}
回答by faran
You can use the getClass()
method, or you can use instanceof. For example
您可以使用该getClass()
方法,也可以使用 instanceof。例如
for (Object obj : list) {
if (obj instanceof String) {
...
}
}
or
或者
for (Object obj : list) {
if (obj.getClass().equals(String.class)) {
...
}
}
Note that instanceof will match subclasses. For instance, of C
is a subclass of A
, then the following will be true:
请注意, instanceof 将匹配子类。例如, ofC
是 的子类A
,那么以下将成立:
C c = new C();
assert c instanceof A;
However, the following will be false:
但是,以下内容将是错误的:
C c = new C();
assert !c.getClass().equals(A.class)
回答by John Gardner
Instanceof works if you don't depend on specific classes, but also keep in mind that you can have nulls in the list, so obj.getClass() will fail, but instanceof always returns false on null.
如果您不依赖于特定的类,Instanceof 就可以工作,但还要记住,您可以在列表中包含空值,因此 obj.getClass() 将失败,但 instanceof 始终在 null 上返回 false。
回答by shoover
You say "this is a piece of java code being written", from which I infer that there is still a chance that you could design it a different way.
您说“这是一段正在编写的 Java 代码”,从中我推断您仍然有可能以不同的方式设计它。
Having an ArrayList is like having a collection of stuff. Rather than force the instanceof or getClass every time you take an object from the list, why not design the system so that you get the type of the object when you retrieve it from the DB, and store it into a collection of the appropriate type of object?
拥有一个 ArrayList 就像拥有一个东西的集合。与其在每次从列表中取出对象时都强制使用 instanceof 或 getClass,为什么不设计系统以便在从 DB 检索对象时获取对象的类型,并将其存储到适当类型的集合中目的?
Or, you could use one of the many data access libraries that exist to do this for you.
或者,您可以使用现有的众多数据访问库之一来为您执行此操作。
回答by Heath Borders
You almost never want you use something like:
您几乎从不希望使用以下内容:
Object o = ...
if (o.getClass().equals(Foo.class)) {
...
}
because you aren't accounting for possible subclasses. You really want to use Class#isAssignableFrom:
因为您没有考虑可能的子类。你真的想使用 Class#isAssignableFrom:
Object o = ...
if (Foo.class.isAssignableFrom(o)) {
...
}
回答by DJClayworth
If you expect the data to be numeric in some form, and all you are interested in doing is converting the result to a numeric value, I would suggest:
如果您希望数据是某种形式的数字,并且您感兴趣的只是将结果转换为数值,我建议:
for (Object o:list) {
Double.parseDouble(o.toString);
}
回答by Reid Mac
In Java just use the instanceof operator. This will also take care of subclasses.
在 Java 中只需使用 instanceof 运算符。这也将照顾子类。
ArrayList<Object> listOfObjects = new ArrayList<Object>();
for(Object obj: listOfObjects){
if(obj instanceof String){
}else if(obj instanceof Integer){
}etc...
}
回答by potter
import java.util.ArrayList;
/**
* @author potter
*
*/
public class storeAny {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Object> anyTy=new ArrayList<Object>();
anyTy.add(new Integer(1));
anyTy.add(new String("Jesus"));
anyTy.add(new Double(12.88));
anyTy.add(new Double(12.89));
anyTy.add(new Double(12.84));
anyTy.add(new Double(12.82));
for (Object o : anyTy) {
if(o instanceof String){
System.out.println(o.toString());
} else if(o instanceof Integer) {
System.out.println(o.toString());
} else if(o instanceof Double) {
System.out.println(o.toString());
}
}
}
}