[英]Java generics: array creation, typecasting and unchecked warnings
我有以下代碼,應該使用不同數字類型(整數,長整數,...)的數組測試不同的排序算法實現(並教我泛型)。 盡管我已經開始使用它了,但是我想知道是否可以進行改進,特別是在我放置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);
}
作為一個很好的一般規則,在可以使用Collection
時,請勿使用數組。
這就是初始化types
變量的方式:
List<Class<? extends Number>> types = Arrays.<Class<? extends Number>>asList( Integer.class, Double.class, Float.class, Long.class, Short.class );
您的其余代碼將保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.