Java 创建未知类型的数组

Java 创建未知类型的数组,java,arrays,generics,Java,Arrays,Generics,我有一个对象,我必须验证这个问题的值,对象的一些属性是自定义对象的数组。这样就需要对数组的各个元素进行一些深入研究。为每个元素执行getter,例如: AttribGrp[] x = Object.getAttribGrp() x[i].getSomeValue() 这就是我要去的地方。我已经使用一个带有列表的枚举来提取数据 属性以以下方式显示 public String getAttribValueAsString(MethodEnum attribName) { String

我有一个对象,我必须验证这个问题的值,对象的一些属性是自定义对象的数组。这样就需要对数组的各个元素进行一些深入研究。为每个元素执行getter,例如:

AttribGrp[] x =  Object.getAttribGrp()
x[i].getSomeValue()
这就是我要去的地方。我已经使用一个带有列表的枚举来提取数据 属性以以下方式显示

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

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

    return attribValue;
}
电话:

    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;
}
我将在数组中处理许多不同类型和不同值的数组。我想创建一个方法,如下所示

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

              return grp;
}

这给了我多个错误,主要与repeatingGrp[]有关。数组类型应与枚举的名称相同。是否可以创建这样一个方法来创建未定义类型的数组?

否,不能将变量(
repeatingGrp
)用作类型

有很多方法可以进行“动态”选角,但这些方法对你没有帮助。
getAttribArray
的返回类型是
Object
,这将破坏对特定类型的强制转换点


即使你能解决这个问题,我们仍然不清楚你能用这个机制做什么。您希望如何处理
getAttribArray()
的结果?

正如Oli Charlesworth指出的,您不能使用变量名进行强制转换。对于泛型类型,必须强制转换为
对象
对象[]

而且
对象
->
对象[]
强制转换看起来是非法的。你可能只是想要一个这样的直演员:

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

如果要使用未知类型的数组,请使用泛型:

 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
 }
public T[]getAttribArray(类repeatingGrpClass)
{
//基于类获取属性(例如,可以基于枚举获取)
return(T[])getAttrib(repeatingGrpClass.getName());//注意,您可能希望在此处使用类对象而不是其名称
}

我不理解“repeatingGrp[]”您将其视为一个类型,但repeatingGrp是一个变量名……OK的可能重复项我可以得到它。但是假设我能够得到传入对象中存在的返回类型:repeatGrp[]。@Will:那又怎么样?然后对结果做什么?我将传递数组,调用每个元素上的各种getter方法来提取值并验证它们。@Will:为什么不使用多态性呢?从公共基类(或接口)派生所有类型,然后让该基类/接口声明一个
validate()
方法。正是这个引擎调用了这个代码。我通过RIORepeatingGrpEnum声明了不同的可用类型,这只是名称,因为这样在运行get-RIORepeatingGrpEnum时就可以提取数组。返回语句中仍然有该对象->对象[]强制转换:)