0

我无法为此正确编写通用方法:

private int [] loadIntArray(String key, int [] defaultArray) {
    int [] array = new int [4];
    for( int index=0; index<4 ; index++) {
        array[index] = sp.getInt(key + index, defaultArray[index]);
    }
    return array;
}

我写了这个:

private <T[]> loadArray(String key, <T[]> defaultArray) {
    <T[]> array = new <T[LEVELS]>;
    for( int index=0; index<4 ; index++) {
        array[index] = sp.getInt(key + index, defaultArray[index]);
    }
    return array;
}

但它不编译。
我收到几个错误,“缺少方法的返回类型”,“私有令牌后预期的类型”。

写它的正确方法是什么?

4

5 回答 5

3

为了实现这一点,您必须在class parameter您的方法中添加一个

Class<T> type

你的方法现在看起来如下

 private <T> T[] loadArray(Class<T> type, String key, T[] defaultArray) {
      T[] array = (T[]) Array.newInstance(type, defaultArray.length);
      for (int index = 0; index < array.length; index++) {
         array[index] = sp.getInt(key + index, defaultArray[index]);
      }
      return array;
   }

问题后更新:
对该方法的调用如何?(例如类型“int”)?

创建 Pair 对象时,不能用原始类型替换类型参数 K 或 V:

Pair<int, char> p = new Pair<>(8, 'a'); // 编译时错误 您只能用非原始类型替换类型参数 K 和 V:

Pair<Integer, Character> p = new Pair<>(8, 'a');

一种可能的解决方法
(以通用方式传递它并将其保留为原始数组的唯一方法是作为对象。)

Object a[]= {1,2,3};
loadArray(Object.class, "test", a);

或者您可以使用非原始类型(整数)

Integer a[]= {1,2,3};
loadArray(Integer.class, "test", a);
于 2013-03-18T13:01:12.883 回答
2

您将返回一个 type 数组T,而不是某个 type T[]

这将更朝着您想要的方向发展。

private <T> T[] loadIntArray(String key, T[] defaultArray) {
    T[] array = new T[4]; // Need more info to correct this
    for (int index = 0; index < 4; index++) {
        array[index] = sp.getInt(key + index, defaultArray[index]);
    }
    return array;
}
于 2013-03-18T12:50:21.247 回答
1

假设defaultArray将永远是nil并且永远是组件类型的数组T,您可以使用它的类型来构造新数组:

import java.lang.reflect.Array;

private <T> T[] loadIntArray(String key, T[] defaultArray) {
    T[] array = (T[])Array.newInstance(defaultArray.getClass().getComponentType(), 4);
    for (int index = 0; index < 4; index++) {
        array[index] = sp.getInt(key + index, defaultArray[index]);
    }
    return array;
}

但是请注意,如果defaultArray的运行时类型是某个子类型的数组T,这将不起作用。它会导致一个ArrayStoreException.

于 2013-03-19T05:40:49.997 回答
0

首先,方法签名应如下所示:

private <T> T[] loadArray(String key, T[] defaultArray)
于 2013-03-18T12:52:40.347 回答
0

这是OP。
虽然这不是标记的答案,但我想发布最终编译的代码,以便那些想要看到它的人。

其他答案中没有提到的一件重要的事情是 Java 泛型不支持只支持原始类型(int、float、...)的对象。所以我不得不将我的数组改为 Integer 和 Float。

private <T> void loadArray(final T[] array, final String key, final T[] defaultArray) {
    int count = array.length;
    for( int index = 0; index < count; index++ ) {
        //if( array[index] instanceof PointF ) {
        if( array instanceof PointF[] ) {
            PointF element = new PointF();
            element.x = sp.getFloat(key + index, (Float) defaultArray[index]);
            element.y = sp.getFloat(key + index, (Float) defaultArray[index]);
            array[index] = (T) element;
        //} else if( array[index] instanceof Float ) {
        } else if( array instanceof Float[] ) {
            Float element = sp.getFloat(key + index, (Float) defaultArray[index]);
            array[index] = (T) element;
        //} else if( array[index] instanceof Integer ) {
        } else if( array instanceof Integer[] ) {
            Integer element = sp.getInt(key + index, (Integer) defaultArray[index]);
            array[index] = (T) element;
        }
    }
}

编辑
事实证明,如果数组是有尺寸的,但没有初始化,

array[index] instanceof Integer  

返回false,所以我将测试更改为

arrray instanceof Integer[]  

true即使数组元素是,它也会返回null

于 2013-03-18T16:48:41.813 回答