我正在嘗試使用 while 回圈將串列串列保存到 ArrayList 中,該回圈遍歷掃描儀中的行。掃描儀正在讀取一個 12 行的二進制文本檔案。串列串列 (ArrayList) 已成功創建,但當 while 回圈終止時,變數 ArrayList 為空并回傳一個空串列串列。我還通過在宣告串列串列的同時宣告一個計數器來測驗代碼,并且計數器在 while 回圈中遞增并在回圈后保留資料。
我對編碼還是很陌生!先感謝您。
public static void main(String[] args) throws Exception{
try {
readFile();
data = dataPrep();
}
catch (Exception e) {
e.printStackTrace ();
}
}
public static void readFile() throws FileNotFoundException {
try {
File inputtxt = new File("test.txt");
scanner = new Scanner(inputtxt);
}
catch (FileNotFoundException error) {
System.out.println(error);
}
}
public static ArrayList<ArrayList> dataPrep(){
ArrayList<ArrayList> allBinaryNumbers = new ArrayList<ArrayList>();
ArrayList<Integer> singleBinaryNumber = new ArrayList<Integer>();
int counter = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
char[] charLine = line.toCharArray();
for (char numb : charLine){
singleBinaryNumber.add(Integer.parseInt(String.valueOf(numb)));
}
allBinaryNumbers.add(singleBinaryNumber);
System.out.println(allBinaryNumbers);
singleBinaryNumber.clear();
counter ;
}
System.out.println(allBinaryNumbers);
System.out.println(counter);
return allBinaryNumbers;
}
我的 test.txt 是這個
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
uj5u.com熱心網友回復:
您正在重復使用singleBinaryNumber完成填充后清除的相同內容。請記住,這是一個參考(指標),這意味著您在每次迭代中添加相同的串列而不是新串列。
你的代碼應該是這樣的:
public static ArrayList<ArrayList> dataPrep(){
ArrayList<ArrayList> allBinaryNumbers = new ArrayList<ArrayList>();
int counter = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
char[] charLine = line.toCharArray();
ArrayList<Integer> singleBinaryNumber = new ArrayList<Integer>(); // create a new list for each iteration
for (char numb : charLine){
singleBinaryNumber.add(Integer.parseInt(String.valueOf(numb)));
}
allBinaryNumbers.add(singleBinaryNumber);
System.out.println(allBinaryNumbers);
// singleBinaryNumber.clear(); <-- remove this line
counter ;
}
System.out.println(allBinaryNumbers);
System.out.println(counter);
return allBinaryNumbers;
}
uj5u.com熱心網友回復:
我認為更好的方法是讓readFile()方法完全做到這一點,讀取檔案而不是僅僅打開它,并讓這個方法回傳一個 ArrayList。它還應該接受一個字串引數,該引數將是檔案路徑(帶有檔案名),而不是直接在方法本身中對檔案路徑進行硬編碼,例如:
// Class instance member variable with Getter & Setter methods.
private String sourceFile;
// In your main() method:
// List of Lists. Each internal list is from a different file.
List<List<String>> filesBinaries = new ArrayList<>();
// List for current file to be read.
List<String> binaries = readFile(sourceFile);
// Add the current file List object to the
// List of Lists (filesBinaries).
if (!binaries.isEmpty()) {
filesBinaries.add(binaries);
}
有了這個,你的readFile()方法可能看起來像這樣:
public static List<String> readFile(String filePath) throws FileNotFoundException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("readFile() Method Error! The "
"supplied file in the path shown below does not exist!" System.lineSeparator()
file.getAbsolutePath() System.lineSeparator());
}
List<String> binaryLines = new ArrayList<>();
// 'Try With Resources' is used here to auto-close the reader.
try (Scanner reader = new Scanner(file)) {
String line = "";
while (reader.hasNextLine()) {
line = reader.nextLine().trim();
// Data Line Validation:
/* Skip past blank lines (if any) and any lines
that do not contain a valid binary string. Valid
lines would be: 100100 or 00100 11100 01110 */
if (line.isEmpty() || !line.matches("[01 ] ")) {
continue;
}
binaryLines.add(line);
}
}
return binaryLines;
}
要觸發此方法,您的main()方法可能如下所示:
public static void main(String[] args) {
List<List<String>> filesData = new ArrayList<>();
String sourceFile = "BinaryData.txt"; // The CURRENT file to read
try {
// Read the desired file...
List<String> binaryData = readFile(sourceFile);
// If the binaryData list is not empty then add it to the fileData List.
if (!binaryData.isEmpty()) {
filesData.add(binaryData);
}
}
catch (FileNotFoundException ex) {
System.err.println("The file: \"" sourceFile "\" could not be "
"found!" System.lineSeparator() ex.getMessage());
}
// Display the contens of the filesData List...
for (int i = 0; i < filesData.size(); i ) {
System.out.println("File #" (i 1) " Binary Data:");
System.out.println("====================");
for (String binaryString : filesData.get(i)) {
System.out.println(binaryString);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414891.html
標籤:
上一篇:通過將陣列B附加到陣列A而不更改陣列A的值,在Julia中定義一個陣列(C)
下一篇:如何對同一個物件進行型別保護?
