如何从 Java 中的对象向量中获取特定类型的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5744197/
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 to get a specific type object from an Object Vector in Java
提问by Mian Asbat Ahmad
I have the following Vector
and at runtime I want to pass it an Integer
, Character
, or Float
:
我有以下内容,Vector
并且在运行时我想传递给它一个Integer
, Character
, 或Float
:
public static Vector<Object> interestingValues = new Vector<Object>();
At the same time I want to get an Integer
, Float
, or Character
from it, so there are two things:
同时我想得到一个Integer
, Float
, , 或者Character
从它,所以有两件事:
Will they retain the specific
Integer
/Float
/Character
type in theObject
Vector
, or they all will be stored in the form ofObject
?How can I retrieve
Integer
objects from thisVector
?
他们将保留特定
Integer
/Float
/Character
在类型Object
Vector
,或者他们都将被存储在形式Object
?如何从中检索
Integer
对象Vector
?
回答by BertNase
Casting:
铸件:
Integer i=(Integer)interestingValues.get(0);
In case you have multiple types of objects inside the Vector you could check the type using instanceof
:
如果 Vector 中有多种类型的对象,您可以使用instanceof
以下命令检查类型:
Object o=interestingValues.get(0);
if (o instanceof Integer){
Integer i=(Integer)o;
}else if (o instanceof Long){
Long l=(Long)o;
}
The third thing you can do to avoid the need ov casting objects to a particular type is using a typed Vector
:
您可以做的第三件事是使用 typed 来避免将对象强制转换为特定类型的需要Vector
:
Vector <Integer> integerVector=new Vector<Integer>();
integerVector.add(Integer.MAX_VALUE);
Integer i=integerVector.get(0);
回答by TerriJordan
Try using the instanceof and casting :
尝试使用 instanceof 和 cast :
if(interestingValues.get(0) instanceof Integer){
Integer value = (Integer) interestingValues.get(0);
}
回答by DwB
instanceof checks the "isa" releationship. If you want to be 100% sure of the class, use getClass(). here is a long winded example:
instanceof 检查“isa”关系。如果您想 100% 确定该类,请使用 getClass()。这是一个冗长的例子:
public class Blue { private String yargh; public Blue() { yargh = "blue"; } public Blue(final String value) { yargh = StringUtils.defaultString(value); } public void println() { System.out.println(yargh); } } public class Berry extends Blue { public Berry() { super("blueberry"); } } ... blah blah blah Vector<Object> stuff = new ... blah blah blah stuff.add(new blue()); stuff.add(new berry()); Iterator<Object> iterator = stuff.iterator(); Object minemine; // this calls println on two objects. while (iterator.hasNext()) { minemine = iterator.next(); if (minemine instanceof Blue) { Blue bluemine = (Blue)minemine; bluemine.println(); } } // This calls println for the Blue object, but not the Berry object. iterator = stuff.iterator(); while (iterator.hasNext()) { minemine = iterator.next(); if (minemine.getClass() == Blue.class) { Blue bluemine= (Berry)minemine; bluemine.println(); } }
Caveat emptor:I did not complile the code above.
注意事项:我没有编译上面的代码。
回答by Alnitak
The Vector
only stores a referenceto the object, not the object itself.
The Vector
only 存储对对象的引用,而不是对象本身。
However since the (non generic) class is defined as storing references to Object
you have to cast the stored references back to the original type before using them:
但是,由于(非泛型)类被定义为存储对Object
您的引用,您必须在使用它们之前将存储的引用转换回原始类型:
String s = (String)myVector.get(n);
That does of course require that you know in advance which particular type is stored in which element...
这当然需要您事先知道哪种特定类型存储在哪个元素中......
You could do:
你可以这样做:
Object o = myVector.get(n);
if (o instance String) {
// do string stuff
} else if (o instanceof Integer) {
// do integer stuff
} etc
回答by Andreas Dolk
You'll have to iterate and check for Integer
instances, for example:
您必须迭代并检查Integer
实例,例如:
List<Integer> integers = new ArrayList<Integer>();
for (Object object:interestingValues) {
if (object instanceof Integer)
integers.add((Integer) object);
}
回答by Thomas
You'd have to check the class of the object:
您必须检查对象的类:
Object obj = myvector.get(0);
if( obj instanceof Integer) {
Integer myInt = (Integer)obj;
}
else if( obj instanceof Character ) {
Integer myChar = (Character)obj;
}
That's not a good design though. You might want to consider storing the objects in different vectors or using a comming representation (e.g. String) along with conversion.
但这不是一个好的设计。您可能需要考虑将对象存储在不同的向量中或使用转换表示(例如字符串)。
Edit:
编辑:
Since Float
, Integer
and the like all extend Number
you might also just check for obj instanceof Number
and then call intValue()
, doubleValue()
etc. on the casted object if all you need is the number and not the type.
由于Float
,Integer
和类似的所有扩展,如果您只需要数字而不是类型,Number
您也可以只检查obj instanceof Number
然后调用intValue()
,doubleValue()
等等 铸造对象。