不同实现的字典;java.lang.ArrayStoreException
所以我有一个项目是关于一个字典来显示一个像普通字典一样的列表。挑战在于,我们应该使用几个实现来显示单词,如treeDictionary、HashMap等不同实现的字典;java.lang.ArrayStoreException,java,arrays,dictionary,implementation,Java,Arrays,Dictionary,Implementation,所以我有一个项目是关于一个字典来显示一个像普通字典一样的列表。挑战在于,我们应该使用几个实现来显示单词,如treeDictionary、HashMap等 启动程序时,我必须选择要使用的实现类型,然后选择data.txt(在这个数据中,第一个数组使用德语单词,第二个数组使用英语单词) 在JTextArea上显示的文字之后,我希望有这个选项,用户不能关闭程序并再次启动它来选择另一个实现和数据等 但是在我选择了第二个实现之后,eclipse向我显示了一个错误 线程“AWT-EventQueue-0”j
{
Component myFrame = null;
static double startTime = 0;
int data[], summeEntry ;
static int size;
static String[] deutsch = new String[16000];
static String[] englisch = new String[16000];
private void ensureCapacity(String o[],int newCapacity){
if(newCapacity < size) {
return;
}
String [] a = o;
data = new int[newCapacity];
System.arraycopy(a, 0, data, 0, size);
}
//open file
public void read(File f) {
LineNumberReader in = null;
try {
in = new LineNumberReader(new FileReader(f));
String line;
int i = 0;
while ((line = in.readLine()) != null) {
String[] sf = line.split(" ");
if (sf.length == 2) {
if(size >= deutsch.length){
ensureCapacity(deutsch, size*2);
ensureCapacity(englisch, size*2);
}
//kamusGUI
kamusGUI.dt.insert(sf[0], sf[1]);
deutsch[i] = sf[0];
englisch[i] = sf[1];
size++;
i++;
} else if (sf.length == 3) { //I just copy from the first if. Then it can calculate the actually number of words
if(size >= deutsch.length){
ensureCapacity(deutsch, size*2);
ensureCapacity(englisch, size*2);
}
kamusGUI.dt.insert(sf[0], sf[1] + sf[2]);
deutsch[i] = sf[0];
englisch[i] = sf[1] + sf[2];
size++;
i++;
} else if(sf.length >= 3){
//wenn ein deutsches Wort mehr als 2 Bedeutungen hat
JOptionPane.showMessageDialog(myFrame, "Hallo, english more than 2 words");
}
}
//to show your entries in output
kamusSearchDelete.tOut.setText(kamusGUI.dt.toString());
//to show how many entries you have from your dictionary
kamusSearchDelete.stsEntry.setText("Total entr(y/ies): " + size + " words");
in.close();
} catch (IOException ex) {
System.out.println("open canceled");
}
}
public static void save(File f) {
PrintWriter out = null;
try {
out = new PrintWriter(f);
out.println(kamusGUI.dt.toString());
out.close();
} catch (IOException ex) {
System.out.println("save canceled");
}
}
问题是您正在从包含
String
引用的一种类型的数组复制到int
值
String [] a = o;
data = new int[newCapacity];
System.arraycopy(a, 0, data, 0, size);
那是行不通的
我应该如何处理数组
您要么需要使数组具有相同的类型,要么必须将一种类型转换/解析为另一种类型
我怀疑没有使用字符串o[]
,而大小
不是容量,而是使用的大小以及您真正希望它根据需要调整数据的大小
private void ensureCapacity(int minCapacity){
if(minCapacity <= data.length)
return;
int[] newData = new int[minCapacity];
System.arraycopy(data, 0, newData , 0, size);
data = newData;
}
private-void-rescapacity(int-minCapacity){
如果(minCapacity您正在调用System.arraycopy(a,0,data,0,size);
a
是一个字符串数组
,而数据
是int数组
。我们无法将字符串数组复制到int数组,因此出现异常。请将带有完整回溯的异常添加到问题本身。您好@peter现在我有另一个问题要显示当前单词的总词条。总数如果我选择另一个实现,条目总是会增加。它只需要显示当前的总数。而不是旧实现+新实现的总数。你有什么想法吗?
private void ensureCapacity(int minCapacity){
if(minCapacity <= data.length)
return;
int[] newData = new int[minCapacity];
System.arraycopy(data, 0, newData , 0, size);
data = newData;
}