简体   繁体   English

当两个列表的大小不同时,将两个列表中具有相同索引的元素的值相加

[英]Add the values of elements with the same index from two lists when the size of both the lists are different

If first.size() is more than second.size() then the resultant list should be of the size of first else it should be the size of second.如果 first.size() 大于 second.size() 那么结果列表应该是 first 的大小,否则它应该是 second 的大小。

import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class Source {
    public static List<Integer> getSumOfListElements(List<Integer> first,List<Integer> second){
     if(first.size()>=second.size()){
        List<Integer> third = new ArrayList<Integer>();
        for (int i = 0; i < first.size(); i++) {
        third.add(first.get(i) + second.get(i));
        }
        return third;
     }else{
      List<Integer> third = new ArrayList<Integer>();
        for (int i = 0; i < second.size(); i++) {
        third.add(first.get(i) + second.get(i));
        }
        return third;
     }
    }

    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       String str = in.nextLine();
       String str2 = in.nextLine();
       System.out.println(str);
       System.out.println(str2);
       List<Integer> list1 = Arrays.stream(str.split("\\s"))
        .mapToInt(Integer::parseInt)
        .boxed()
        .collect(Collectors.toList());
       List<Integer> list2 = Arrays.stream(str2.split("\\s"))
        .mapToInt(Integer::parseInt)
        .boxed()
        .collect(Collectors.toList());
       System.out.println(list1.size());
       System.out.println(list2.size());
       List<Integer> actual = Source.getSumOfListElements(list1,list2);
       System.out.println(actual);
    }
}

This is the code that I have written.这是我写的代码。 It is giving exception Operation not permitted.它给出了不允许的异常操作。

Your code does not work because when you try to add two numbers from lists (say first list size=20, and second list size=10), at one point i become larger than second list.您的代码不起作用,因为当您尝试从列表中添加两个数字时(比如第一个列表大小 = 20,第二个列表大小 = 10),在某一时刻i变得比第二个列表大。 When your for loop try to find element for that i , you will get an error.当您的 for 循环尝试为该i查找元素时,您将收到错误消息。

To eradicate this, you should use a try catch block to bypass addition and add remaining elements of large list directly.要消除这种情况,您应该使用 try catch 块来绕过添加并直接添加大列表的剩余元素。

Take a look at the following code:看看下面的代码:

public static List<Integer> getSumOfListElements(List<Integer> first, List<Integer> second) {

        // declare 'third' list with size of max('first' list, 'second' list)
        List<Integer> third = new ArrayList<>(Math.max(first.size(), second.size()));

        // loop through max('first' list, 'second' list) number elements in both lists
        for (int i = 0; i < Math.max(first.size(), second.size()); i++) {
            try {
                third.add(first.get(i) + second.get(i));

                // at one point either first or second will be finished.
                // (say second is finished at this point)
                // then add remaining elements of first to third
            } catch (IndexOutOfBoundsException e) {
                if (first.size() > second.size()) third.add(first.get(i));
                else third.add(second.get(i));
            }
        }
        return third;
    }

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

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