创建一个未知类型的数组 [英] Creating an Array of unknown type

查看:87
本文介绍了创建一个未知类型的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我必须验证问题值的对象,这些对象的某些属性是自定义对象的数组.这样一来,这将涉及到数组的各个元素.提取每个元素的吸气剂,例如:

I have an object which I must validate values off the problem, some of the attributes of the Objects are arrays of custom objects. Such that it will involve some boring down into the individual elements of the array. Excuting the getters for each element such as:

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

这是我需要了解的.我已经使用带有列表的Enum提取了数据 属性的方式如下.

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;
}

致电:

    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. 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;
}

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

This is giving me multiple errors mainly concerned with repeatingGrp[]. 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:

 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
 }

这篇关于创建一个未知类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆