简体   繁体   English

创建一个未知类型的数组

[英]Creating an Array of unknown type

I have an object which I must validate values off the problem, some of the attributes of the Objects are arrays of custom objects.我有一个 object,我必须验证问题的值,对象的一些属性是自定义对象的 arrays。 Such that it will involve some boring down into the individual elements of the array.这样它就会涉及到对数组的各个元素进行一些无聊的处理。 Excuting the getters for each element such as:为每个元素执行 getter,例如:

AttribGrp[] x =  Object.getAttribGrp()
x[i].getSomeValue()

It is this I need to get to.这是我需要达到的。 I have been extracted the data using an Enum with the list of the attributes In the following manner.我已经使用带有属性列表的枚举以下列方式提取了数据。

public String getAttribValueAsString(MethodEnum attribName) 
{
    String attribValue = null;
    Object value = getAttrib(attribName.toString());

    if (value != null)
        attribValue = value.toString();

    return attribValue;
}

calling:调用:

    private Object invoke(String methodName, Object newValue)
{
    Object value = null;
    try
    {
        methodInvoker.setTargetMethod(methodName);

        if (newValue != null)
            methodInvoker.setArguments(new Object[]{newValue});
        else
            methodInvoker.setArguments(new Object[]{});             

        methodInvoker.prepare();
        value = methodInvoker.invoke();
    }
    catch (ClassNotFoundException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    catch (NoSuchMethodException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    catch (InvocationTargetException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    catch (IllegalAccessException e)
    {
        throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
    }
    return value;
}

I will be working with a number of arrays of different types and of different values within the arrays.我将在 arrays 中使用许多不同类型和不同值的 arrays。 I want to create a method as follows.我想创建一个方法如下。

    public Object getAttribArray(RIORepeatingGrpEnum repeatingGrp)
{
        repeatingGrp[] grp = null;
              Object grpVal = getAttrib(repeatingGrp.toString());
              if(grp != null)
                   grp = (repeatingGrp[]) grpVal;

              return grp;
}

This is giving me multiple errors mainly concerned with repeatingGrp[].这给了我多个主要与重复 Grp [] 有关的错误。 The array type should be the same name as enum.数组类型应与枚举同名。 Is it possible to create a method like this that will create an array of non defined type?是否可以创建一个这样的方法来创建一个未定义类型的数组?

If you want to have arrays of unknown types, use generics:如果您想拥有未知类型的 arrays,请使用 generics:

 public <T> T[] getAttribArray(Class<T> repeatingGrpClass)
 {
    //get the attribute based on the class (which you might get based on the enum for example)
    return (T[]) getAttrib( repeatingGrpClass.getName() ); //note that you might want to use the class object instead of its name here
 }

No, you cannot use a variable ( repeatingGrp ) as a type.不,您不能将变量( repeatingGrp )用作类型。

There are ways to do "dynamic" casting, but these wouldn't help you.有一些方法可以进行“动态”投射,但这些对你没有帮助。 The return type of getAttribArray is Object , which would defeat the point of casting to a particular type. getAttribArray的返回类型是Object ,这会破坏转换为特定类型的点。

And even if you could fix that, it's still not clear what you could do with this mechanism.即使你能解决这个问题,仍然不清楚你能用这个机制做什么。 What do you want to be able to do with the result of getAttribArray() ?您希望能够对getAttribArray()的结果做什么?

As Oli Charlesworth points out, you can not use the variable name to cast.正如 Oli Charlesworth 指出的那样,您不能使用变量名进行转换。 For a generic type, you will have to cast to Object or Object[] .对于泛型类型,您必须强制转换为ObjectObject[]

Also the Object -> Object[] cast looks illegal.此外Object -> Object[]演员看起来是非法的。 You probably just want a straight cast like so:您可能只想要这样的直接演员:

public Object[] getAttribArray(RIORepeatingGrpEnum repeatingGrp)
{
    Object[] grp = (Object[])getAttrib(repeatingGrp.toString());
    return grp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM