我嘗試在我的迭代器中實現hasNext()方法。如何將char []數組的內容添加到新的char []字母中? - java
public boolean hasNext() {
filename = this.fileContent.getFileName();
int charLineSize = 0;
Scanner in = null;
ArrayList<Character> charList = new ArrayList<>
try(Scanner scanner = new Scanner(new BufferedReader(new FileReader(filePath)))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
charLineSize = line.split("\\s+").length;
String wordsline = line.split("\\s+").toLowerCase();
charList.addAll(new ArrayList<>(wordsline.chars().mapToObj(c -> (char) c).collect(Collectors.toList())));
}
} catch(FileNotFoundException e) {
System.out.println(e);
}
if (index < charLineSize) {
return true;
} else {
return false;
}
}
public String next() {
if (hasNext()) {
return charList.get(index++);
} else {
return null;
}
}
首先,我在這裏將字符分割爲字符。將所有字符存儲在charList中並在掃描程序遍歷每行後更新它是否正確?
我想在Next()方法中獲取特定的char。
你爲什麼不直接分割線的字符,並把他們的的char []數組中使用.toCharArray()? – Debabrata