CSV 파일을 Java의 2D 배열로 변환하는 데 문제가 있습니다. 이 문제를 해결하는 데 가장 길게 갈 수도 있지만 왜 오류가 발생하는지 파악할 수는 없습니다. 각 행과 열에는 각각 25 개의 요소가 있어야합니다. 여기 내 코드는 다음과 같습니다.CSV 파일을 2D 배열로 변환 Java
BufferedReader CSVFile = new BufferedReader(new FileReader(fileName));
String dataRow = CSVFile.readLine();
// Read first line.
// The while checks to see if the data is null. If
// it is, we've hit the end of the file. If not,
// process the data.
while (dataRow != null) {
dataRow.split(",");
list.add(dataRow);
dataRow = CSVFile.readLine();
// Read next line of data.
}
// Close the file once all data has been read.
CSVFile.close();
String[] tokens = null;
Object[] test = list.toArray();
String[] stringArray = Arrays.copyOf(test, test.length, String[].class); //copies the object array into a String array
//splits the elements of the array up and stores them into token array
for (int a = 0; a < test.length; a++) {
String temp = stringArray[a];
tokens = temp.split(",");
}
//converts these String tokens into ints
int intarray[] = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
intarray[i] = Integer.parseInt(tokens[i]);
}
//attempts to create a 2d array out of a single dimension array
int array2d[][] = new int[10][3];
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
array2d[i][j] = intarray[(j * 25) + i];
}
}
나는 ArrayList가 첫 번째 String 배열로 복사되지만 확신 할 수없는 오류라고 생각합니다. 이 파일에는 25 개의 열과 25 개의 행이 있습니다. 내가 계속 오류 배열은 인덱스 25에서 범위를 벗어났다는 것입니다. 어떤 입력 크게 감사하겠습니다. 감사!
목록은 무엇인가? 나는 그것이 선언 된 것을 보지 못한다. –
어떤 배열입니까? array2d 또는 intarray? 어떤 라인? – Dan
데이터 행 루프에서 Object가 필요하다고 생각하지 않습니다. readLine은 문자열을 반환합니다. –