In this problem, I have to add elements from lists and print them using another list. I think I am doing it wrong because it gave me an empty list. I am new to java programming. Please help me out.
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class Source {
public List<Integer> getSumOfListElements(List<Integer> first,List<Integer> second){
List<Integer>result=new ArrayList<Integer>();
int i;
for(i=0;i<first.size();i++){
result.add(first.get(i)+second.get(i));
}
return result;
}
public static void main(String[] args) {
//CODE HERE
Source s= new Source();
List<Integer>a=new ArrayList();
List<Integer>b=new ArrayList();
Scanner sc=new Scanner(System.in);
while (s.hasNextInt()) {
int i = sc.nextInt();
a.add(i);
}
while (sc.hasNextInt()) {
int i = sc.nextInt();
b.add(i);
}
List result1=s.getSumOfListElements(a,b);
System.out.print(result1);
}
}
input
10, 20, 30, 40, 50
12, 23, 34
The output should be like this:
[22, 43, 64, 40, 50]