你可以传递一个int数组到Java中的泛型方法吗? [英] Can you pass an int array to a generic method in java?

查看:99
本文介绍了你可以传递一个int数组到Java中的泛型方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一些代码katas,并试图同时更好地理解java泛型。我有这样一个打印数组的方法,就像我喜欢看到它们一样,并且我有几个帮助器方法,它们接受'东西'和索引的数组,并返回索引上方或下方'东西'的数组这是一个二进制搜索算法)。



两个问题,

#1我可以避免在splitBottom和splitTop中投射到T吗?它感觉不对,或者我正在以这种错误的方式去做(不要告诉我使用python或其他东西......))

#2我必须编写单独的方法来处理基本数组吗?还是有更好的解决方案?

  public class Util {

public static< T> void print(T [] array){
System.out.print({);
for(int i = 0; i< array.length; i ++){
System.out.print(array [i]);
if(i< array.length - 1){
System.out.print(,);
}
}
System.out.println(});
}

public static< T> T [] splitTop(T [] array,int index){
Object [] result = new Object [array.length - index - 1];
System.arraycopy(array,index + 1,result,0,result.length);
return(T [])结果;
}

public static< T> T [] splitBottom(T [] array,int index){
Object [] result = new Object [index];
System.arraycopy(array,0,result,0,index);
return(T [])结果;


public static void main(String [] args){

Integer [] integerArray = {1,2,3,4,5,6, 7,8,9,10};
print(integerArray);
print(splitBottom(integerArray,3));
print(splitTop(integerArray,3));

String [] stringArray = {one,two,three,four,five,six,seven,eight,nine, 十};
print(stringArray);
print(splitBottom(stringArray,3));
print(splitTop(stringArray,3));

int [] intArray = {1,2,3,4,5,6,7,8,9,10};
// ???
}
}


解决方案

泛型不要以一致的方式处理原语。这是因为泛型不像C ++中的模板,它只是编译时添加到一个类中。


$ b 当通用编译完成后,最终会以Object []在上面的例子中作为实现类型。作为int []和byte []等,不要扩展Object [],即使所涉及的代码是相同的(即泛型不是模板),也不能互换使用它们。

唯一的类int []和Object []共享是Object。你可以把上面的方法Object写成类型(参见System.arraycopy,Array.getLength,Array.get,Array.set)。

I'm playing around with some code katas and trying to get a better understanding of java generics at the same time. I've got this little method that prints arrays like I like to see them and I have a couple of helper methods which accept an array of 'things' and an index and returns the array of the 'things' above or below the index (it's a binary search algorithm).

Two questions,

#1 Can i avoid the cast to T in splitBottom and splitTop? It doesn't feel right, or I'm going about this the wrong way (don't tell me to use python or something .. ;) )

#2 Do I have to write seperate methods to deal with primitive arrays or is there a better solution?

public class Util {

    public static <T> void print(T[] array) {
        System.out.print("{");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if (i < array.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("}");
    }

    public static <T> T[] splitTop(T[] array, int index) {
        Object[] result = new Object[array.length - index - 1];
        System.arraycopy(array, index + 1, result, 0, result.length);
        return (T[]) result;
    }

    public static <T> T[] splitBottom(T[] array, int index) {
        Object[] result = new Object[index];
        System.arraycopy(array, 0, result, 0, index);
        return (T[]) result;
    }

    public static void main(String[] args) {

        Integer[] integerArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        print(integerArray);
        print(splitBottom(integerArray, 3));
        print(splitTop(integerArray, 3));

        String[] stringArray = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
        print(stringArray);
        print(splitBottom(stringArray, 3));
        print(splitTop(stringArray, 3));

        int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        // ???
    }
}

解决方案

Generics don't handle primitives in a consistent fashion. This is because Generics are not like templates in C++, it is just a compile time addition to a single class.

When generic are compiled, you end up with Object[] in the above example as the implementing type. As int[] and byte[] etc, do not extend Object[] you cannot use them inter-changeably even if the code involved would be identical (again generics are not templates)

The only class int[] and Object[] share is Object. You can write the above methods Object as the type (see System.arraycopy, Array.getLength, Array.get, Array.set)

这篇关于你可以传递一个int数组到Java中的泛型方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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