In: Computer Science
Using Java with no imports,
Method arrayOfSums returns an ArrayList of the same
size as the largest ArrayList
This method needs to be overloaded
so that it works for 2 to 4 sent Arraylists.
arrayOfSums([2,4,7], [3,7,14])
returns [5,11,21]
arrayOfSums([6,13,4,8], [7,5,9],
[2,5,8,3]) returns [15, 23, 21, 11]
arrayOfSums([6,13,4,8], [7,5,9],
[2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]
arrayOfSums([8,6], [2,4,10])
returns [10,10,10]
arrayOfSums([2,2,2,2,2],
[3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8, 7, 6]
arrayOfSums([11,12,13,14],
[14,13,12,11], [10,10,10,10], [5,6,7,6,5]) returns [40, 41, 42, 41,
5]
@param nums1 ArrayList of integers
of varying sizes
@param nums2 ArrayList of integers
of varying sizes
@param nums3 ArrayList of integers
of varying sizes
@param nums4 ArrayList of integers
of varying sizes
@return an array of the sums of the
corresponding elements from each array
*/
public static ArrayList<Integer>
arrayOfSums(ArrayList<Integer> nums1,
ArrayList<Integer> nums2, ArrayList<Integer> nums3,
ArrayList<Integer> nums4 )
{
ArrayList<Integer> numsSums =
new ArrayList<Integer>();
return numsSums;
}// end arrayOfSums
//Overloaded methods here to compile
public static
ArrayList<Integer> arrayOfSums(ArrayList<Integer>
nums1, ArrayList<Integer> nums2, ArrayList<Integer>
nums3)
{
ArrayList<Integer> numsSums = new
ArrayList<Integer>();
return numsSums;
}// end arrayOfSums
public static
ArrayList<Integer> arrayOfSums(ArrayList<Integer>
nums1, ArrayList<Integer> nums2)
{
ArrayList<Integer> numsSums = new
ArrayList<Integer>();
return numsSums;
}// end arrayOfSums
public class ArrayListSums {
public static void main(String[] args) {
java.util.ArrayList list1 = new java.util.ArrayList();
list1.add(2);
list1.add(4);
list1.add(7);
java.util.ArrayList list2 = new java.util.ArrayList();
list2.add(3);
list2.add(7);
list2.add(14);
System.out.println(arrayOfSums(list1,list2));
}
public static java.util.ArrayList<Integer> arrayOfSums(java.util.ArrayList<Integer> nums1,
java.util.ArrayList<Integer> nums2, java.util.AbstractList<Integer> nums3,
java.util.ArrayList<Integer> nums4) {
java.util.ArrayList<Integer> numsSums = new java.util.ArrayList<Integer>();
// iterating through arraylist and getting elemenet from each list and adding to result
for (int i = 0; i < nums1.size(); i++) {
int total=0;
//checking if num1 has valid index
if(nums1.size()>i)
total+=nums1.get(i);
//checking if num2 has valid index
if(nums2.size()>i)
total+=nums2.get(i);
//checking if num3 has valid index
if(nums3.size()>i)
total+=nums3.get(i);
//checking if num4 has valid index
if(nums4.size()>i)
total+=nums4.get(i);
numsSums.add(total);
}
return numsSums;
}// end arrayOfSums
// Overloaded methods here to compile
public static java.util.ArrayList<Integer> arrayOfSums(java.util.ArrayList<Integer> nums1,
java.util.ArrayList<Integer> nums2, java.util.ArrayList<Integer> nums3) {
java.util.ArrayList<Integer> numsSums = new java.util.ArrayList<Integer>();
// iterating through arraylist and getting elemenet from each list and adding to result
for (int i = 0; i < nums1.size(); i++) {
int total=0;
//checking if num1 has valid index
if(nums1.size()>i)
total+=nums1.get(i);
//checking if num2 has valid index
if(nums2.size()>i)
total+=nums2.get(i);
//checking if num3 has valid index
if(nums3.size()>i)
total+=nums3.get(i);
numsSums.add(total);
}
return numsSums;
}// end arrayOfSums
public static java.util.ArrayList<Integer> arrayOfSums(java.util.ArrayList<Integer> nums1,
java.util.ArrayList<Integer> nums2) {
java.util.ArrayList<Integer> numsSums = new java.util.ArrayList<Integer>();
// iterating through arraylist and getting elemenet from each list and adding to result
for (int i = 0; i < nums1.size(); i++) {
int total=0;
//checking if num1 has valid index
if(nums1.size()>i)
total+=nums1.get(i);
//checking if num2 has valid index
if(nums2.size()>i)
total+=nums2.get(i);
numsSums.add(total);
}
return numsSums;
}// end arrayOfSums
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot