java需要帮助/无法重载泛型方法
我需要重载GenericMethodtest的泛型方法printArray,以便
it takes two additional integer arguments,
lowsubscript
andhighsubscript
. A call to this method prints only the designated portion of the array. Validatelowsubscript
andhighsubscript
. if either is out of range, the overloadedprintarray
method should throw aninvalidsubscriptexception
; otherwise,printArray
should return the number of elements printed.Then modify main to exercise both verisons of
printArray
on arraysintegerArray
,doubleArray
andcharacterArray
. Test all capabilities of both versions ofprintArray
.
这就是我到目前为止所做的,我被卡住了,不知道从哪里开始
public class GenericMethodTest
{
public static void main(String[] args)
{
// create arrays of Integer, Double and Character
Integer[] integerArray = {1, 2, 3, 4, 5, 6};
Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
Character[] characterArray = {'H', 'E', 'L', 'L', 'O'}
System.out.printf("%nArray integerArray contains:%n");
printArray(integerArray); // pass an Integer array
System.out.printf("%nArray doubleArray contains:%n");
printArray(doubleArray); // pass a Double array
System.out.printf("%nArray characterArray contains:%n");
printArray(characterArray); // pass a Character array
}
// generic method printArray
public static <T> void printArray(T[] inputArray)
{
// display array elements
for (T element : inputArray)
System.out.printf("%s ", element);
System.out.println();
}
} // end class GenericMethodTest
# 1 楼答案
公共类打印{
}