package genericMethods;
public class OverloadedMethods {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] integerArray = {1, 2, 3, 4, 5};
Double[] doubleArray = {1.0, 2.0, 3.0, 4.0, 5.0};
Character[] charArray = {'a', 'b', 'c', 'd'};
System.out.println("A has: ");
printArray(integerArray);
System.out.println("B has: ");
printArray(doubleArray);
System.out.println("C has: ");
printArray(charArray);
}
public static <T> void printArray(T[] array)
{
for(T element : array)
System.out.printf("%s ", element);//why use %s instead of others?
System.out.println();
}
}
我的问题是方法 printArray() 不知道要打印出什么类型的数据,并且似乎 %d 会在运行时发生错误-但不是 %s 仅用于 String 吗?