[英]Java generics: array creation, typecasting and unchecked warnings
I have following code, which is supposed to test different sorting algorithm implementations (& teach me generics) with arrays of different number types (Integer, Long, ...). 我有以下代码,应该使用不同数字类型(整数,长整数,...)的数组测试不同的排序算法实现(并教我泛型)。 Although I've gotten it to work, I was wondering if there can be improvements, specifically in 3 places where I've put
FIXME
. 尽管我已经开始使用它了,但是我想知道是否可以进行改进,特别是在我放置
FIXME
三个地方。
Thanks.. 谢谢..
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();
// }
}
}
---- edit ---- ----编辑----
class
above is meant to test following interface
: 上面的
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);
}
As a good general rule, never use an array when you could use a Collection
. 作为一个很好的一般规则,在可以使用
Collection
时,请勿使用数组。
This is how I would initialize the types
variable: 这就是初始化
types
变量的方式:
List<Class<? extends Number>> types = Arrays.<Class<? extends Number>>asList( Integer.class, Double.class, Float.class, Long.class, Short.class );
The rest of your code will work unchanged. 您的其余代码将保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.