1
我有班級其成員是byte[]
我需要把班級的對象TreeMap
與key
作爲這個班的對象。以下是我寫的,但put
和get
不按預期工作。樹形圖和字節[]
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class XTObject Comparable<XTObject>{
public byte[] data;
public XTObject(){
}
public XTObject(final byte[] in) {
this.data = in;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
XTObject other = (XTObject) obj;
if (!Arrays.equals(data, other.data))
return false;
return true;
}
@Override
public int hashCode() {
return Arrays.hashCode(data);
}
@Override
public int compareTo(XTObject o) {
ByteBuffer left = ByteBuffer.wrap(this.data);
ByteBuffer right = ByteBuffer.wrap(o.data);
return left.compareTo(right);
}
}
認沽工作正常我猜,但get
正在給空指針異常。我是新來的Java。
代碼放在哪裏? – 2013-03-19 07:40:00
也許這是因爲如果使用無參數構造函數,則不會初始化字段'data',因此data == null – dantuch 2013-03-19 07:40:45
您如何使用樹形圖? – Gothmog 2013-03-19 07:41:48