您可以执行以下操作:
String [] wordList;int wordCount = 0;int occurrence = 1;int arraySize = 100;int arrayGrowth = 50;wordList = new String[arraySize];while ((strLine = br.readLine()) != null) { // Store the content into an array Scanner s = new Scanner(strLine); while(s.hasNext()) { if (wordList.length == wordCount) { // expand list wordList = Arrays.copyOf(wordList, wordList.length + arrayGrowth); } wordList[wordCount] = s.next(); wordCount++; } }使用
java.util.Arrays.copyOf(String[])基本上与执行以下操作:
if (wordList.length == wordCount) { String[] temp = new String[wordList.length + arrayGrowth]; System.arraycopy(wordList, 0, temp, 0, wordList.length); wordList = temp;}除了它是一行代码而不是三行代码。:)



