【发布时间】:2012-11-26 04:10:38
【问题描述】:
Integer[] intArray = new Integer[] {1,2,3,4};
Haha.sort(intArray);
public class Haha<E extends Comparable<? super E>> implements B<E>
{
private E[] array;
public Haha()
{
array = (E[]) new Comparable[10];
}
private Haha( ??? )
{
???
}
public static <T extends Comparable<? super T>> void sort (T[] a)
{
Haha(a);
}
}
我想定义一个私有构造函数 static sort() 方法可以调用来创建一个用特定给定数组初始化的哈哈对象。
但它必须对其参数数组进行排序而不分配另一个数组,因为公共构造函数已经在分配另一个数组。
问题 1) 私人哈哈如何从 sort() 方法中接收“a”? 如果我这样做了
private Haha(E[] b) // gives error because of different type T & E
{
// skip
}
如果我这样做
private Haha(T[] b) // gives error too because Haha is class of Haha<E>
{
// skip
}
问题 2) 如何使用特定的给定数组初始化对象
private Haha( ??? b ) // if problem 1 is solved
{
array = b; // is this right way of initializing array, not allocating?
// It also gives error because sort() is static, but Haha is not.
}
【问题讨论】:
-
接口
B是什么?它与问题相关吗? -
你确定你的任务不是对给定的数组进行排序吗?
-
@Bohemian B
b1 = new haha (); // BTester 类内部 -
@Perception 对不起。我不明白你的问题。对给定的数组进行就地排序??
标签: java initialization