How to get a specific type object from an Object Vector in Java
I have the following Vector
and at runtime I want to pass it an Integer
, Character
, or 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:
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
?
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
:
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
:
Vector <Integer> integerVector=new Vector<Integer>();
integerVector.add(Integer.MAX_VALUE);
Integer i=integerVector.get(0);
Try using the instanceof and casting :
if(interestingValues.get(0) instanceof Integer){
Integer value = (Integer) interestingValues.get(0);
}
The Vector
only stores a reference to the object, not the object itself.
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:
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
You'll have to iterate and check for Integer
instances, for example:
List<Integer> integers = new ArrayList<Integer>();
for (Object object:interestingValues) {
if (object instanceof Integer)
integers.add((Integer) object);
}
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.
instanceof checks the "isa" releationship. If you want to be 100% sure of the class, use getClass(). here is a long winded example:
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.
精彩评论