그래서 길이가 200 인 큰 문자열 배열을 반환하려고하지만 사용중인 코드는 15를 반환합니다./실행 중에 배열 길이를 15로 설정하면 200 공간을 예약 할 수 있습니다. 배열의 크기가 변경 될 수 있으므로 다른 크기를 수용하기 위해 큰 크기로 만들었습니다.버퍼링 된 텍스트 작성자
public static String[] populateArray(String fileName)
{
String [] record = new String [200]; // array length is 200
try
{
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
int counter = 0;
int arrayLength = 0; // using this to check length during execution
while((line = bufferedReader.readLine())!= null && counter < 200)
{
record[counter] = line;
counter +=1;
arrayLength = record[0].length(); // the file contains 20 lines to input here but
only does 15
}
bufferedReader.close();
}
catch (IOException ex)
{
System.out.println("There was an error while trying to access the file");
}
return record;
}
루프 내부의'arrayLength = record [0] .length()'의 목적은 무엇입니까? 'record [0]'는 첫 번째 반복에서 할당되고 그 이후에는 절대 업데이트되지 않으므로 루프 외부에서 해당 행을 이동할 수 있습니다. 아니면'arrayLength'가 첫 번째 입력 행의 길이와 관련이있는 이유를 찾지 못해서 뭔가를 할 수 있습니다. * * –
Andreas