如何从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:33:23  来源:igfitidea点击:

How to get a specific type object from an Object Vector in Java

javaobjectvectorfloating-pointinteger

提问by Mian Asbat Ahmad

I have the following Vectorand 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 Characterfrom it, so there are two things:

同时我想得到一个Integer, Float, , 或者Character从它,所以有两件事:

  • Will they retain the specific Integer/Float/Charactertype in the ObjectVector, or they all will be stored in the form of Object?

  • How can I retrieve Integerobjects from this Vector?

  • 他们将保留特定Integer/ Float/Character在类型ObjectVector,或者他们都将被存储在形式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 Vectoronly stores a referenceto the object, not the object itself.

The Vectoronly 存储对对象的引用,而不是对象本身。

However since the (non generic) class is defined as storing references to Objectyou 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 Integerinstances, 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, Integerand the like all extend Numberyou might also just check for obj instanceof Numberand 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()等等 铸造对象。