如何为每行打印内容创建List/ArrayList?如何存储每行内容至不同数据结构?
Hey there! Let's adjust your existing code to meet your requirement of storing each line from the CSV file into separate list structures. I'll cover two common scenarios based on what you might mean:
Scenario 1: Store each entire line in its own separate List
If you want every single line from the file to be held in an individual List (even if each list only contains one element), here's how you can modify your code:
We'll use a List<List<String>> to hold all these individual line lists. This way, each entry in the outer list is a dedicated List for one line of the file.
import java.io.File; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class ReadFile { private Scanner x; // Field to hold all line-specific lists private List<List<String>> allLineLists = new LinkedList<>(); public void openFile() { try { x = new Scanner(new File("C:\\Users\\kakos\\Downloads\\data.csv")); } catch(Exception e) { System.out.println("Could not find file: " + e.getMessage()); } } public void readFile() { // Avoid null pointer if file failed to open if (x == null) { System.out.println("File not opened properly."); return; } while(x.hasNextLine()) { String line = x.nextLine(); System.out.println(line); // Keep your existing print logic // Create a new List for this line and add the line content List<String> singleLineList = new LinkedList<>(); singleLineList.add(line); // Add the line-specific list to our outer collection allLineLists.add(singleLineList); } } public void closeFile() { // Only close scanner if it's initialized if (x != null) { x.close(); } } // Optional: Retrieve all stored line lists public List<List<String>> getAllLineLists() { return allLineLists; } }
Scenario 2: Split CSV fields into separate column-specific Lists
Since you're reading a CSV file, you probably want to split each line into individual fields and store all values from the same column into a dedicated List (e.g., all first-column values in one List, all second-column values in another). Here's how to implement that:
import java.io.File; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class ReadFile { private Scanner x; // List to hold each column's values (each entry is a List for one column) private List<List<String>> columnLists = new LinkedList<>(); public void openFile() { try { x = new Scanner(new File("C:\\Users\\kakos\\Downloads\\data.csv")); } catch(Exception e) { System.out.println("Could not find file: " + e.getMessage()); } } public void readFile() { if (x == null) { System.out.println("File not opened properly."); return; } while(x.hasNextLine()) { String line = x.nextLine(); System.out.println(line); // Keep printing the line // Split CSV line into fields (adjust separator if your CSV uses something else) String[] fields = line.split(","); // Initialize column lists on the first line of the file if (columnLists.isEmpty()) { for (int i = 0; i < fields.length; i++) { columnLists.add(new LinkedList<>()); } } // Add each field to its corresponding column list for (int i = 0; i < fields.length; i++) { // Prevent index out of bounds if lines have varying field counts if (i < columnLists.size()) { columnLists.get(i).add(fields[i].trim()); // Trim extra whitespace from fields } } } } public void closeFile() { if (x != null) { x.close(); } } // Optional: Retrieve all column-specific lists public List<List<String>> getColumnLists() { return columnLists; } }
Key Improvements in Both Solutions:
- Added null checks for the
Scannerto avoidNullPointerExceptionif the file fails to open. - Updated error messaging to be more descriptive.
- Included methods to retrieve the stored lists so you can use the data elsewhere in your code.
内容的提问来源于stack exchange,提问作者Alexandros Loukas

