我有以下代码,它应该使用不同数字类型(整数、长整数、...)的数组来测试不同的排序算法实现(并教我泛型)。虽然我已经让它工作了,但我想知道是否可以改进,特别是在我放置FIXME
.
谢谢..
package com.kash.test;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
class SortTester<E extends Number> {
E[] arr;
E[] copyArr;
Class<E> classOfElemType;
int arrayLenToTestWith;
@SuppressWarnings({ "unchecked" })
SortTester(Class<E> cc, int ll) {
classOfElemType = cc;
arrayLenToTestWith = ll;
arr = (E[]) Array.newInstance(classOfElemType, arrayLenToTestWith);
copyArr = (E[]) Array.newInstance(classOfElemType, arrayLenToTestWith);
for (int i = 0; i < arrayLenToTestWith; i++) {
arr[i] = copyArr[i] = randomNumber();
}
}
void reset() {
System.arraycopy(copyArr, 0, arr, 0, arrayLenToTestWith);
}
E randomNumber() {
// FIXME: Is tehre a way to do this without reflection?
// FIXME: Also by converting the random() output to int we lose
// precision, anyway to avoid that without some "instanceof"
// kinda checks?
try {
// Every Number subclass, has a c'tor with a single String
// arg, which represents an int.
Constructor<E> ctor = classOfElemType.getConstructor(String.class);
return ctor.newInstance(Integer.toString((int) (Math.random() * (arrayLenToTestWith * 2))));
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException
| IllegalArgumentException | InvocationTargetException e) {
System.err.println(e.getMessage());
e.printStackTrace();
e.getCause().printStackTrace();
}
return null;
}
void print() {
for (int i = 0; i < arrayLenToTestWith; i++) {
System.out.println(Array.get(arr, i));
}
}
void test(/* sorters */) {
// test the Sorter instances on our array for time and space..
// for s in sorters
// do
// // start monitors..
// s.sort(arr);
// // stop monitors
// reset();
// done
// // print results...
}
}
public class Main<T extends Number> {
public static void main(String[] args) {
// FIXME: Is tehre a way to remove the unchecked warnings for Class below? Without
// using "@SuppressWarnings({ "unchecked" })"
Class types[] = { Integer.class, Double.class, Float.class, Long.class, Short.class };
for (Class cc : types) {
SortTester<?> typeTester = new SortTester<>(cc, 10);
System.out.println("Type = " + cc.getCanonicalName());
typeTester.print();
}
// Class<?> types2[] = { Integer.class, Double.class, Float.class, Long.class, Short.class };
// for (Class<?> cc1 : types) {
// SortTester<cc1> typeTester = new SortTester<>(cc1, 10);
// typeTester.print();
// }
}
}
- - 编辑 - -
class
以上旨在测试以下内容interface
:
package com.kash.src;
import java.util.Comparator;
import java.util.List;
/**
*
* Interface that provides all sorting functions for all possible representations
* of a list of Objects (non-primitive types). <br>
*
* - {@link List}<{@link Comparable}><br>
* - Array of < ? extends {@link Comparable}><br>
* - {@link List}< ? > with external {@link Comparator}<br>
* - Array of < ? > with external {@link Comparator}<br>
*
* @author Kashyap Bhatt
*
* @param <T>
*/
public interface Sorter {
<T extends Comparable<? super T>> void sort(List<T> list);
<T extends Comparable<? super T>> void sort(T[] array);
<T extends Object> void sort(List<T> list, Comparator<? super T> c);
<T extends Object> void sort(T[] array, Comparator<? super T> c);
}