此代码旨在执行搜索和替换array
使用另一个的 2 列array.
目前它IndexOutOfBoundsException
在这一行返回 a:
fileContents.set(i, fileContents.get(i).replace(hexLibrary[i][0], hexLibrary[i][1]));
我是 Java 新手,所以我不太了解它的语法是如何工作的,无法找出问题所在。整个搜索和替换代码块是:
String[][] hexLibrary; // calls the replaces array from the LibToArray method
hexLibrary = LibToArray();
for(int i=0;i<hexLibrary.length;i++) {
fileContents.set(i, fileContents.get(i).replace(hexLibrary[i][0], hexLibrary[i][1]));
}
for (String row : fileContents) {
System.out.println(row); // print array to cmd
}
构建array
用于执行替换的代码是:
String thisLine;
String[] temp;
String delimiter=",";
String [][] hexLibrary = new String[501][2];
try {
BufferedReader br= new BufferedReader(new FileReader("hexlibrary.txt"));
for (int j=0; j<501; j++) {
thisLine=br.readLine();
if (thisLine != null) {
temp = thisLine.split(delimiter);
for (int i = 0; i < 2; i++) {
hexLibrary[j][i]=temp[i];
}
} else {
JOptionPane.showMessageDialog(null,"Library file corrupt.");
break; // no point in continuing to loop
}
}
}
- - - - 编辑 - - - -
下面是初始化“fileContents”的代码array.
String FileName; // set file variable
FileName = fileName.getText(); // get file name
ArrayList<String> fileContents = new ArrayList<String>(); // create arraylist
BufferedReader reader = new BufferedReader(new FileReader(FileName)); // create reader
String line = null;
while ((line = reader.readLine()) != null) {
if(line.length() > 0){ // don't include blank lines
line = line.trim(); // remove whitespaces
fileContents.add(line); // add to array
}
}