如何将整数数组用于泛型方法? [英] How to use an integer array for a generic method?
问题描述
我的插入排序方法是使用以下语句声明的:
My insertion sort method is declared using this statement:
public static <AnyType extends Comparable<? super AnyType>>
void insertionSort(AnyType[] a){
我很难理解如何制作此通用数组类型参数接受原始类型 int
的数组。
I am having trouble understanding how to make this generic array type parameter accept an array of primitive type int
.
我如何用<$调用此方法c $ c> int [] 作为我的参数,或者至少如何将其用于对 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?
推荐答案
使用包装器类-整数。有一种叫做自动装箱的东西,这意味着如果您传递原始类型int的参数,它将自动转换为Integer:-)
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
这篇关于如何将整数数组用于泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!