How to use an integer array for a generic method?

♀尐吖头ヾ 提交于 2020-03-26 06:08:06

问题


My insertion sort method is declared using this statement:

    public static <AnyType extends Comparable<? super AnyType>> 
          void insertionSort(AnyType[] a){

I am having trouble understanding how to make this generic array type parameter accept an array of primitive type int.

How would I call this method with int[] as my parameter, or at least how would it be used to sort an int array?


回答1:


Use a wrapper class - Integer. There is something called autoboxing which means that if you pass an argument of primitive type int it will convert automatically to Integer :-)

http://en.wikipedia.org/wiki/Primitive_wrapper_class https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html



来源:https://stackoverflow.com/questions/30008538/how-to-use-an-integer-array-for-a-generic-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!