You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
654 B
24 lines
654 B
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class GenericUtils {
|
|
// 泛型方法:打印数组元素
|
|
public static <E> void printArray(E[] array) {
|
|
for (E element : array) {
|
|
System.out.print(element + " ");
|
|
}
|
|
System.out.println();
|
|
}
|
|
|
|
// 泛型方法:交换数组中的两个元素
|
|
public static <T> void swap(T[] array, int i, int j) {
|
|
T temp = array[i];
|
|
array[i] = array[j];
|
|
array[j] = temp;
|
|
}
|
|
|
|
// 泛型方法:计算列表中的元素数量
|
|
public static <E> int countListElements(List<E> list) {
|
|
return list.size();
|
|
}
|
|
}
|