I am trying to get the sum of two array lists elements with a different amount of elements in each one in a method. So far I have been able to get the elements to add but when the results print, it leaves out numbers because the array lists have different amount of elements. Lets say array list a has 5 elements and array list b has 3 elements. The missing amounts in the array are supposed to be 0. So if a has {1, 2, 3, 4, 5} and b has {2, 3, 1}. The result should be {3, 5 , 4, 4, 5}.
public static ArrayList<Integer> sum(ArrayList<Integer> a, ArrayList<Integer> b)
{ ArrayList result = new ArrayList ();
for(int i = 0; i < a.size(); i ) {
result.add(a.get(i) b.get(i));
}
return result;
}
CodePudding user response:
Note that you have 2 arrays in different sizes, so you should take it into consideration and your loop can't be always on a
size, since it might be bigger than b
size and then you try to access elements that aren't exist.
Something you can do is to loop over elements which are exist in both arrays, and then complete the rest. Suggestion:
public static ArrayList<Integer> sum(ArrayList<Integer> a, ArrayList<Integer> b) {
ArrayList result = new ArrayList();
int aSize = a.size();
int bSize = b.size();
for (int i = 0; i < Math.min(aSize, bSize); i ) { // Loop over elements that exist in both arrays
result.add(a.get(i) b.get(i));
}
if (aSize > bSize) { // Complete missing from a
for (int i = aSize - bSize 1; i < aSize; i ) {
result.add(a.get(i));
}
} else if (bSize > aSize) { // Complete missing from b
for (int i = bSize - aSize 1; i < bSize; i ) {
result.add(b.get(i));
}
}
return result;
}
CodePudding user response:
Take two lists add each elements one by one and store there sum in new list if there is no more element in any of the list take the remaining elements from the larger list and append at the end of the new list.
import java.util.*;
public class MainSum{
public static void main(String ... $){
var out = System.out;
List<Integer> a = new ArrayList<>(List.of(1, 2, 3, 4, 5));
List<Integer> b = new ArrayList<>(List.of(2, 3, 1));
List<Integer> result = MainSum.sum(a, b);
out.println(result);
}
public static List<Integer> sum(List<Integer> a, List<Integer> b){
List<Integer> result = new ArrayList<>();
for(int index =0 ;index<a.size() && index< b.size();index )
result.add(a.get(index) b.get(index));
if( a.size() != b.size())
result.addAll(a.size()>b.size()?a.subList(b.size(), a.size()):b.subList(a.size(), b.size()));
return result;
}
}
Output:
[3, 5, 4, 4, 5]
CodePudding user response:
You have to make sure to iterate to the higher index, and make sure you don't run into a problem with a non existing index.
BiFunction<ArrayList<Integer>, Integer, Integer> getSafe =
(l, i ) -> i < l.size() ? l.get(i) : 0;
for (int i = 0; i < Math.max(list1.size(), list2.size(); i ) {
result.add(getSafe.apply(list1) getSafe.apply(list1));
}
CodePudding user response:
Another approach can be based on customizing each list return values.
public class TestArrSum {
public static void main(String[] args)
{
List<Integer> a = List.of(1,2,3,4,5);
List<Integer> b = List.of(2,3,1);
List<Integer> result = sum(a,b);
result.forEach(System.out::println);
}
public static List<Integer> sum(List<Integer> a, List<Integer> b)
{
List<Integer> result = new ArrayList<Integer>();
for(int i=0;i<a.size() || i<b.size();i )
{
result.add(getValue(a,i) getValue(b,i));
}
return result;
}
public static int getValue(List<Integer> l,int i)
{
try
{
return l.get(i);
}
catch(ArrayIndexOutOfBoundsException e)
{
return 0;
}
}
}
Note: One advantage can be in terms of verbosity in case if need that method signature to contain more lists to be sum.
CodePudding user response:
You should compare the size of two lists to determine which list will be iterated.
public static List<Integer> sum(List<Integer> a, List<Integer> b) {
List<Integer> result;
if (a.size() > b.size()) {
result = new ArrayList<>(a);
for (int i = 0; i < b.size(); i ) {
result.set(i, a.get(i) b.get(i));
}
} else {
result = new ArrayList<>(b);
for (int i = 0; i < a.size(); i ) {
result.set(i, a.get(i) b.get(i));
}
}
return result;
}