我正在编写一个通用最大堆的简单实现.如果我写
public class FastMaxHeap<T>{
T[] data;
int size;
static final int HEAP_SIZE = 10000;
@SuppressWarnings("unchecked")
public FastMaxHeap(){
data = (T[]) new Object[HEAP_SIZE];
size = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
它汇编.现在要实际实现堆,即写入maxHeapify(),我需要能够比较两个T.先验似乎可能的一个选择是告诉编译器T实现Comparable.但是,如果我用<T implements Comparable>键入replace <T>,编译器会抱怨 - 我该怎么做?
或者,我可以定义一个类
public class HasValue{
int value;
public HasValue(int value){
this.value = value;
}
Run Code Online (Sandbox Code Playgroud)
}
理论上我应该能够比较两个HasValue对象,如x.value> y.value.但如果我输入
public class FastMaxHeap<T extends HasValue>{
T[] data;
int size;
static final int HEAP_SIZE = 10000;
@SuppressWarnings("unchecked")
public FastMaxHeap(){
data = (T[]) new Object[HEAP_SIZE];
size = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我现在得到一个ClassCastException.这里发生了什么?Java泛型伤害了我的大脑.
可能重复:
使用Java创建通用数组
我想将一般的对象数组修剪为只有第一个len元素.这似乎应该工作:
@SuppressWarnings("unchecked")
public static <T> T[] trimArray(T[] data, int len){
T[] result = (T[]) new Object[len];
System.arraycopy(data, 0, result, 0, len);
return result;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我做类似的事情,它会引发ClassCastException
public class Foo{
double f;
public Foo(double f){
this.f = f;
}
}
public static void main(String[] args){
Foo[] A = new Foo[10];
A[0]= new Foo(1);
A[1]= new Foo(2);
A[2]= new Foo(3);
Foo[] B = trimArray(A, 3);
}
Run Code Online (Sandbox Code Playgroud)
我发现自己经常犯以下错误.
public class Foo{
Bar bar;
public void doSomething(){
Bar bar = makeNewBar();
// bla bla
}
}
Run Code Online (Sandbox Code Playgroud)
错误在于我想设置Foo的栏,而不是本地栏.即,我应该说bar = makeNewBar(),不是Bar bar = makeNewBar().是否有编译器警告标志或可以检测到"命名空间冲突"的东西?如果不是,那么避免这种情况的好方法是什么(没有犯错误,这显然是最好的选择= p)?
我正在编写数字代码,其中定义向量操作很有用.例如,如果x和y是充满浮点数的n长向量,那么让x ^ y导致y的第i个元素中的a等于x的第i个元素的某个任意函数是很好的.一个简单的方法是:
#include <vector>
#include <stdio.h>
#include <ctime>
using namespace std;
template <typename T>
void operator^(vector<T> A, vector<T> B){
typename vector<T>::iterator a = A.begin();
typename vector<T>::iterator b = B.begin();
while(a!=A.end()){
*b = 2*(*a);
a++; b++;
}
//for (uint i=0; i<A.size(); i++)
//B[i] = 2*A[i];
}
int main(int argc, char** argv){
int n = 10000;
int numRuns = 100000;
vector<float> A;
for (int i=0; i<n; i++)
A.push_back((float) i);
vector<float> B = vector<float>(n);
clock_t t1 = clock();
for (int i=0; i<numRuns; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个通用的编码器/解码器,并遇到了扩展通用的问题.我的想法是,我希望有一个抽象的Encodeable类,它具有一个"虚拟"静态方法解码,它接受一个Byte []并构造该对象,类似于Serializable.(我知道这不能在java中完成.)每个扩展Encodeable的类都会覆盖编码/解码方法.然后我想一般使用这些Encodeable的子类.这是试图表明我的意思:
public class Encodeable{
// I'd like to write
// static abstract Encodeable decode(Byte[]);
// similar to
// virtual Encodeable decode(Byte[]) = 0;
// in C++, but that seems to be illegal in java
static Encodeable decode(Byte[] buf){return null};
}
public class EncodeableFoo extends Encodeable{
static EncodeableFoo decode(Byte[] buf){
// do actual decoding logic here
}
}
public class Bar<T extends Encodeable>{
public void messageReceived(MessageEvent e){
Byte[] buf = e.getMessage();
T messageObj = T.decode(buf);
// do something with T …Run Code Online (Sandbox Code Playgroud) java ×4
generics ×3
casting ×2
algorithm ×1
c++ ×1
namespaces ×1
operators ×1
optimization ×1
scope ×1
types ×1