简体   繁体   English

Java文本文件中的简单列表

[英]Simple List from a text file with Java

I'm currently studying and also I'm new with Java programming, I have a question about nodes and simple lists. 我正在学习,并且对Java编程还是陌生的,我对节点和简单列表有疑问。

I have to read two files that looks like this L1: 1, 5, 8, 4 and L2: 4, 8, 9, 4 and store them in a List not an Array . 我必须读取两个看起来像L1: 1, 5, 8, 4文件:1、5、8、4和L2: 4, 8, 9, 4并将它们存储在List而不是Array I wrote some code but it's not what I was supposed to do, so if you could help me to understand Nodes and Lists I'd really appreciate it. 我写了一些代码,但这不是我应该做的,因此,如果您可以帮助我理解Nodes和Lists,我将非常感激。

Also after reading the files, another list has to be generated with the sum of the other two lists. 同样,在读取文件之后,必须将其他两个列表的总和生成另一个列表。 This sum has to be with the positions of the list so it should look like this L3: 5, 13, 17, 8 此总和必须与列表的位置一致,因此它应类似于以下L3: 5, 13, 17, 8

This is the code I used: 这是我使用的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;

public class Read {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        FileReader fr, fr2;
        try {
            fr = new FileReader(new File("list1.txt"));
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();

            fr2 = new FileReader(new File("list2.txt"));
            BufferedReader br2 = new BufferedReader(fr2);
            String line2 = br2.readLine();

            StringTokenizer st = new StringTokenizer(line, ", ");
            int dimension = st.countTokens();
            int sum = 0;
            int sum2 = 0;
            int total = 0;

            int[] arrNum = new int[dimension];

            while (st.hasMoreTokens()) {
                System.out.print("List 1: ");
                for (int i = 0; i < arrNum.length; i++) {
                    arrNum[i] = Integer.parseInt(st.nextToken());
                    System.out.print(arrNum[i] + ", ");
                    sum += arrNum[i];
                }
            }
            System.out.println("\nThe sum of the first list is: " + sum + "\n");

            StringTokenizer st2 = new StringTokenizer(line2, ", ");
            int dimension2 = st2.countTokens();

            int[] arrNum2 = new int[dimension2];

            while (st2.hasMoreTokens()) {
                System.out.print("List 2: ");
                for (int j = 0; j < arrNum2.length; j++) {
                    arrNum2[j] = Integer.parseInt(st2.nextToken());
                    System.out.print(arrNum2[j] + ", ");
                    sum2 += arrNum2[j];
                }
            }
            System.out.println("\nThe sum of the second list is: " + sum2);

            total = sum + sum2;
            System.out.println("\nThe sum of the lists are: " + total);

            br.close();
            br2.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I hope you can tell me how to do this with the List and Nodes. 希望您能告诉我如何使用列表和节点。

请参阅LinkedList 文档示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM