java - java中逐列求和

标签 java arrays io

i have file txt in desktop :
1   5     23
2   5     25  
3   30    36

i want sum column by column 1 + 2 + 3 =... and 5 + 5...n and 23,...n

Scanner sc = new Scanner (file("patch");
while (sc.hasNextLine)

{

//each sum by column

}

help me please thanks

最佳答案

我会使用 try-with-resources 清理我的Scanner使用File 。另外,您可以构造一个 Scanner周围line输入以获得您的 int列(不需要关闭,因为 String (s) 无论如何都不可关闭)。比如,

try (Scanner sc = new Scanner(new File("patch"))) {
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        Scanner row = new Scanner(line);
        long sum = 0;
        int count = 0;
        while (row.hasNextInt()) {
            int val = row.nextInt();
            if (count == 0) {
                System.out.print(val);
            } else {
                System.out.printf(" + %d", val);
            }
            sum += val;
            count++;
        }
        System.out.println(" = " + sum);
    }
} catch (IOException e) {
    e.printStackTrace();
}

作为 Scanner(String) 构造函数Javadoc文档

Constructs a new Scanner that produces values scanned from the specified string.

编辑 对列进行求和有点棘手,但您可以将所有内容读入多维 List<List<Integer>>喜欢

try (Scanner sc = new Scanner(new File("patch"))) {
    List<List<Integer>> rows = new ArrayList<>();
    int colCount = 0;
    while (sc.hasNextLine()) {
        List<Integer> al = new ArrayList<>();
        String line = sc.nextLine();
        Scanner row = new Scanner(line);
        colCount = 0;
        while (row.hasNextInt()) {
            colCount++;
            int val = row.nextInt();
            al.add(val);
        }
        rows.add(al);
    }
    for (int i = 0; i < colCount; i++) {
        long sum = 0;
        for (List<Integer> row : rows) {
            sum += row.get(i);
        }
        if (i != 0) {
            System.out.print("\t");
        }
        System.out.print(sum);
    }
    System.out.println();
} catch (IOException e) {
    e.printStackTrace();
}

编辑 2 为了提高效率,您可能更愿意使用 Map喜欢

try (Scanner sc = new Scanner(new File("patch"))) {
    Map<Integer, Integer> cols = new HashMap<>();
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        Scanner row = new Scanner(line);
        int colCount = 0;
        while (row.hasNextInt()) {
            int val = row.nextInt();
            if (cols.containsKey(colCount)) {
                val += cols.get(colCount);
            }
            cols.put(colCount, val);
            colCount++;
        }
    }
    for (int i : cols.values()) {
        System.out.printf("%d\t", i);
    }
    System.out.println();
} catch (IOException e) {
    e.printStackTrace();
}

关于java - java中逐列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26826151/

相关文章:

c# - 为什么我刚刚删除后不能创建txt文件?

java - 在释放按钮之前不要单击

Java:从输入文件填充二维数组

arrays - 对象数组的 Json 架构未验证

.net - File.Exists是一项昂贵的操作吗?

python - 如何下载 .data 文件并将其转换为 .csv?

java - 是否可以强制解析由 java ".jar"文件创建的窗口?

java - AWS Lambda 和 Java 反射 (Guava)

javascript - 如何通过现有数组标签明智地收集/制作新数组?

c - C中的数组数据类型