【发布时间】:2021-12-31 04:20:16
【问题描述】:
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader reader = null;
ArrayList<String> lines = new ArrayList<String>();
try{
reader = new BufferedReader(new FileReader("Numbers.txt"));
String currentLine = reader.readLine();
}
catch (IOException e) {
System.out.println("ERROR: WRONG FILE " + e.toString());
}
String currentLine = reader.readLine();
while (currentLine != null){
lines.add(currentLine);
currentLine = reader.readLine();
Collections.sort(lines);
ArrayList[] linesSorted = lines.toArray(new ArrayList[lines.size()]);
System.out.println("Sorted Array: " + Arrays.toString(linesSorted));
}
}
}
当我输入此代码时,该代码旨在从另一个文件中读取单词列表,将其转换为数组,然后对所述数组进行排序,弹出错误消息说
Exception in thread "main" java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, java.util.ArrayList
at java.base/java.lang.System.arraycopy(Native Method)
at java.base/java.util.ArrayList.toArray(ArrayList.java:433)
at Main.main(Main.java:24)
我不太确定此错误消息是什么意思,或者我该如何解决它。帮助!我对计算机编码很陌生,所以我对该主题的了解并不多。
【问题讨论】:
标签: java