에 요소를 추가하는 데 :어려움 나는 다음과 같다 .txt 파일이 2 차원 배열
Mathematics:MTH105
Science:SCI205
Computer Science:CPS301
...
을 그리고 내가 파일을 읽고 배열로 각 선을 배치해야 할당이를이해야 다음과 같이 : 내가 컴파일 오류를 얻고있다
subjectsArray[][] = {
{"Mathematics", "MTH105"},
{"Science", "SCI205"},
{"Computer Science", "CPS301"}
};
을 나는 2 차원 배열에 파일의 내용을 추가 할 때 :
private static String[][] getFileContents(File file) {
Scanner scanner = null;
ArrayList<String[][]> subjectsArray = new ArrayList<String[][]>();
//Place the contents of the file in an array and return the array
try {
scanner = new Scanner(file);
int i = 0;
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] lineSplit = line.split(":");
for(int j = 0; j < lineSplit.length; j++) {
subjectsArray[i][j].add(lineSplit[0]); //The type of the expression must be an array type but it resolved to ArrayList<String[][]>
}
i++;
}
return subjectsArray;
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
scanner.close();
}
return null;
}
오류가 읽
The type of the expression must be an array type but it resolved to ArrayList<String[][]>
는 나는 내가 잘못이 무엇인지를 여러 차원 배열 새로운 모르겠습니다. 누군가 내가 뭘 잘못하고 있다고 말할 수 있습니까?
코드에서 오류에 관해 질문 할 때 전체 오류 메시지를 게시하고 코드에 표시하는 것이 현명하고 도움이됩니다. 어느 선이 그것을 일으키는 지. 이 정보를 포함시켜야한다는 것이 분명한 것 같습니다. –
@HovercraftFullOfEels 예 : subjectsArray [i] [j] .add (lineSplit [0]); // 표현식 유형은 배열 유형이어야하지만 ArrayList로 해석되어야합니다. –
@NicholasLaw 독자가 코드를 찾을 필요가 없도록 코드 외부에 두는 것이 좋습니다. – awksp