如何使用該副本從TreeSet
打印重複項?使用TreeSet中的副本並將副本打印出來
我創建了一個方法,允許我填充一個沒有文本文件重複的數組,現在我需要讓這些重複文件在另一個文件中編寫它們。我怎麼做?
// method that gets that reads the file and puts it in to an array
public static void readFromfile() throws IOException {
// Open the file.
File file = new File("file.txt");
Scanner inputFile = new Scanner(file);
// create a new array set Integer list
Set<Integer> set = new TreeSet<Integer>();
// add the numbers to the list
while (inputFile.hasNextInt()) {
set.add(inputFile.nextInt());
}
// transform the Set list in to an array
Integer[] numbersInteger = set.toArray(new Integer[set.size()]);
// loop that print out the array
for (int i = 0; i < numbersInteger.length; i++) {
System.out.println(numbersInteger[i]);
}
// close the input stream
inputFile.close();
}
使用代碼塊一致性和邏輯縮進。代碼的縮進旨在幫助人們理解程序流程! –
使用'set.add(inputFile.nextInt());' –