将多个arraylist整数添加到一起

时间:2014-10-13 22:01:25

标签: java arraylist


更新:

使用Mike Kobit的回答进行更改后,我想将我已经计算过的文件移动到另一个文件夹。文件没有成功移动,我不知道为什么?是否与锁定文件的数组列表有关?

public static void main(String[] args) throws FileNotFoundException {

    fSplit(path);   

    Date date = new Date();
    SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd_HHmmSS");
    String d_f = d.format(date);

    System.out.println(allSums);

    try {
        File fold = new File(path);
        for(File k : fold.listFiles()) {
            System.out.println(k.getName());
            if(k.getName().contains("file")) { //named files read to be moved
                boolean success = k.renameTo(new File(path2 + "\\" + k.getName()));
                if(!success) {
                    System.out.println("FAILED MOVE");
                }
            }
        }
    } catch(Exception e) { e.printStackTrace(); }
}

private static void fSplit(String path) throws FileNotFoundException {
    //ArrayList<Integer> allSums = new ArrayList<>();
    //ArrayList<List<Integer>> allLists = new ArrayList<>();

    File folder = new File(path);

    for (File f : folder.listFiles()) {
        //System.out.println(f.getName());

        BufferedReader br = new BufferedReader(new FileReader(path + "\\" + f.getName()));
        Scanner scanner = new Scanner(br);

        ArrayList<Integer> list = new ArrayList<Integer>();

        while(scanner.hasNext()) {
            list.add(scanner.nextInt());
        }
        // Store each read list into a container for all of the lists
        allLists.add(list);

        //System.out.println(list);
    }

    // Assuming all lists are the same size
    //int listLength = allLists.get(0).size();

    // Iterate over each index
    for(int i = 0; i < 5; i++) {
        int sum = 0;
        // For each index, add the elements from that index in each list
        for(List<Integer> list : allLists) {
            sum += list.get(i);
        }
        // Add the current indexes sum to the final list
        allSums.add(sum);
    }

}

3 个答案:

答案 0 :(得分:1)

您需要跟踪数组,然后可以迭代它们。以下是添加注释的示例以及如何在以后总结元素。

ArrayList<Integer> allSums = new ArrayList<>();
ArrayList<List<Integer>> allLists = new ArrayList<>();
for (File f : folder.listFiles()) {

    BufferedReader br = new BufferedReader(new FileReader(path + "\\" + f.getName()));
    Scanner scanner = new Scanner(br);
    ArrayList<Integer> list = new ArrayList<Integer>();
    while (scanner.hasNext()) {
        list.add(scanner.nextInt());
    }
    // Store each read list into a container for all of the lists
    allLists.add(list);
    System.out.println(list);
}

// Assuming all lists are the same size
final int listLength = allLists.get(0).size();
// Iterate over each index
for (int i = 0; i < listLength; i++) {
    int sum = 0;
    // For each index, add the element from that index in each list
    for (List<Integer> list : allLists) {
        sum += list.get(i);
    }
    // Add the current indexes sum to the final list
    allSums.add(sum);
}
// allSums contains the sum from every index

// Using Java 8 streams
allSums.clear();
IntStream.range(0, listLength)
        .forEach((i) -> allSums.add(i, allLists.stream().collect(Collectors.summingInt((c) -> c.get(i)))));
System.out.println(allSums);

两种方式的输出:     [11,12,6,5,11]     [11,12,6,5,11]

答案 1 :(得分:0)

解释:在for循环中,您将在不同的数组中相互添加所有相同的索引元素,并将其保存在结果数组的相同索引中

注意

我的代码是使用数组完成的,但我相信你可以很容易地将其更改为arraylist

<强>代码

   List<Integer> list1 = Arrays.asList(5, 4, 3, 1, 0);
   List<Integer> list2 = Arrays.asList(3, 4, 2, 1, 5);
   List<Integer> list3 = Arrays.asList(3, 4, 1, 3, 6);

    List<Integer> result = new ArrayList<>();

    // you cannot use reslut size because result does not have anything in it, so 
    // there is no size for result list.
    // you can use size each list as long as the size of all the list is the same

    for (int i = 0; i < list1.size(); i++) {
        result.add(i, list1.get(i) + list2.get(i) + list3.get(i));
    }

    for (int i = 0; i < result.length; i++) {
        System.out.print(result.get(i)+" ");
    }

<强>输出

11 12 6 5 11

答案 2 :(得分:0)

如果你想使用ArrayList添加,你可以这样做

    List<Integer> sum = new ArrayList<Integer>(5);

    for (int i = 0; i < sum.size(); i++) {
        sum.add(i, list1.get(i) + list2.get(i) + list3.get(i));
    }