【问题标题】:How can I remove specific elements from a linkedlist in java based on user input?java - 如何根据用户输入从java中的链表中删除特定元素?
【发布时间】:2023-04-08 16:55:02
【问题描述】:

我是新手(进入 java 6 周)试图从 csv 文件中删除元素,该文件列出了一组学生,例如(id, name, grades),每个都在一个新行上。

每个学生id 都按升序编号。我想尝试通过输入身份证号码来删除学生,但我不知道该怎么做。

到目前为止,我只是尝试减少用户输入的值以匹配索引,因为学生按数字列出,我在 while 循环中执行了此操作。但是,每次迭代都无法识别先前用户输入的减少,我认为我需要一种可以仅搜索 id 值并从 csv 文件中删除整行的方法。

仅尝试包含相关代码。阅读以前的堆栈问题向我展示了一堆与节点相关的答案,这对我来说毫无意义,因为我没有理解它所需的任何先决知识,而且我不确定我的其余代码是否适用那些方法。

有什么比较简单的想法吗?

Student.txt(各换行)

1,Frank,West,98,95,87,78,77,80
2,Dianne,Greene,78,94,88,87,95,92
3,Doug,Lei,78,94,88,87,95,92
etc....

代码:

public static boolean readFile(String filename) {
    File file = new File("C:\\Users\\me\\eclipse-workspace\\studentdata.txt");
    try {
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine()) {
            String[] words=scanner.nextLine().split(",");

            int id = Integer.parseInt(words[0]);
            String firstName = words[1];
            String lastName = words[2];
            int mathMark1 = Integer.parseInt(words[3]);
            int mathMark2 = Integer.parseInt(words[4]);
            int mathMark3 = Integer.parseInt(words[5]);
            int englishMark1 = Integer.parseInt(words[6]);
            int englishMark2 = Integer.parseInt(words[7]);
            int englishMark3 = Integer.parseInt(words[8]);

            addStudent(id,firstName,lastName,mathMark1,mathMark2,mathMark3,englishMark1,englishMark2,englishMark3);

        }scanner.close();

}catch (FileNotFoundException e) {
    System.out.println("Failed to readfile.");

    private static void removeStudent() {
        String answer = "Yes";
        while(answer.equals("Yes") || answer.equals("yes")) {
            System.out.println("Do you wish to delete a student?");
            answer = scanner.next();
            if (answer.equals("Yes") || answer.equals("yes")) {
                System.out.println("Please enter the ID of the student to be removed.");
                 //tried various things here: taking userInput and passing through linkedlist.remove() but has never worked.

【问题讨论】:

  • 提供studentdata.txt示例文件,包含少量记录。
  • 1,Frank,West,98,95,87,78,77,80 2,Dianne,Greene,78,94,88,87,95,92 3,Doug,Lei,78, 94,88,87,95,92
  • 您是否尝试过使用任何 Java 库(如 CSVReader 或 Apache Commons CSV)来实现它

标签: java linked-list


【解决方案1】:

这个解决方案可能不是最佳的或漂亮的,但它确实有效。它逐行读取输入文件,将每一行写入临时输出文件。每当遇到与您要查找的内容匹配的行时,它都会跳过写出该行。然后它重命名输出文件。我在示例中省略了错误处理、关闭读取器/写入器等。我还假设您要查找的行中没有前导或尾随空格。根据需要更改 trim() 周围的代码,以便找到匹配项。

File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String lineToRemove = "bbb";
String currentLine;

while((currentLine = reader.readLine()) != null) {
    // trim newline when comparing with lineToRemove
    String trimmedLine = currentLine.trim();
    if(trimmedLine.equals(lineToRemove)) continue;
    writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close(); 
reader.close(); 
boolean successful = tempFile.renameTo(inputFile);

【讨论】:

  • 谢谢你,但是,我只想编辑链表,直到很久以后才一定会影响 studentdata.txt 文件。因此,数据最初被提取并保存到一个临时链表中,该链表被读取和编辑(并从另一种方法写入文件)。虽然从内存的角度来看它显然不是最佳的,但这个列表非常小。
  • 你说的是什么链表,你的代码没有显示任何链表,能否提供你说的包含链表的代码
  • 对不起。读取文件并提取每个名称等并保存为名为students的临时链接列表。 LinkedList 本身仅在对其执行功能以转换为等级等时才会打印出来。但列表 LinkedList 本身是从 csv 文件派生的。 LinkedList 称为 LinkedList 学生; 派生自另一个类,该类为从文件中读取每个学生时要执行的每个方法设置参数。因此,该列表实际上仅在读取和输出 csv 文件时提供。我不确定我是否解释得很好。
  • 从链表中删除元素非常容易。像这样使用 Java 流:list.stream.filter(e-> e.getId()!=unwantedValue).collect(Collectors.toList());
猜你喜欢
  • 2018-07-08
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 2023-04-10
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多