Java Program to Compute the Sum of Numbers in a List Using Recursion
Last Updated :
23 Apr, 2023
ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package.
Illustration:
Input : [1, 3, 9]
Output : 13
Here the naive method can be to add up elements of List and maintain a counter in which sum is stored while traversing List. The step ahead method can be to convert the List to the array and do the same. Now more optimal method can be to use recursion while doing so in which a subpart of a List or array is automatically computed by recursion principles. Here this optimal approach is described and implemented as shown.
Methods:
- Converting ArrayList to arrays and using recursion principles over arrays.
- Using ArrayList.add() method
Method 1: Converting ArrayList to arrays and using recursion principles over arrays.

It is achieved by converting ArrayList to arrays and using recursion principles over arrays. Recursion in the list to array conversion and computing sum of elements using add() method.
Approach:
- Take the elements of the list as input from the user.
- Convert the list into an array of the same size.
- Add the elements to it.
- Compute the sum of arrays using recursion principles.

Example
Java
// Java Program to Compute Sum of Numbers in a List
// by converting to arrays and applying recursion
// Importing java input/output classes
import java.io.*;
// Importing List and ArrayList class from
// java.util package
import java.util.ArrayList;
import java.util.List;
// Class
public class GFG {
// Method to calculate sum recursively
public static int sumOfArray(Integer[] a, int n)
{
if (n == 0)
return a[n];
else
return a[n] + sumOfArray(a, n - 1);
}
// Method- main()
public static void main(String[] args)
{
// Creating a List of Integer type
// Declaring an object- 'al'
List<Integer> al = new ArrayList<Integer>();
// Adding elements to the List
// Custom inputs
al.add(1);
al.add(2);
al.add(3);
al.add(4);
al.add(5);
// Converting above List to array
// using toArray() method
Integer a[] = new Integer[al.size()];
al.toArray(a);
// Display message
System.out.print("Elements in List : ");
// Printing array of objects
// using for each loop
for (Integer obj : a) {
System.out.print(obj + " ");
}
// Recursion math to calculate sum snd
// storing sum in a variable
int sum = sumOfArray(a, a.length - 1);
// Next line
System.out.println();
// Print the sum returned above
System.out.println("Sum of elements : " + sum);
}
}
OutputElements in List : 1 2 3 4 5
Sum of elements : 15
The time complexity is O(n), where n is the size of the input list.
The auxiliary space is also O(n), where n is the size of the input list.
Method 2: Using ArrayList.add() method
This method appends the specified element to the end of this list
Syntax:
public boolean add(E element) ;
Parameter: Object to be appended to this list.
Return Type: It will always return a boolean true and the signature is as so because other classes in collections family need a return type.
Exceptions: NA
Example:
Java
// Java Program to Compute the Sum of Numbers in a List
// using Recursion via ArrayList.add() method
// Importing all classes of
// java.util package
import java.util.*;
// Class
public class GFG
{
// Declaring variables outside main class
int sum = 0, j = 0;
// Main driver method
public static void main(String[]args)
{
/*
// Taking the input from the user
int n;
Scanner s = new Scanner(System.in);
// Display message
System.out.print("Enter the no. of elements :");
// Reading integer elements using nextInt() method
n = s.nextInt();
// Display message
System.out.println("Enter all the elements you want:");
*/
// Creating an object of List of Integer type
List < Integer > list = new ArrayList < Integer > ();
// Adding elements to object of List
// Custom inputs to show sum
list.add(10);
list.add(90);
list.add(30);
list.add(40);
list.add(70);
list.add(100);
list.add(0);
System.out.println("Elements in List : " + list);
/*
// If input is through user than
// For loop to add elements inside List
for (int i = 0; i < n; i++)
{
// Adding integer elements in the list
list.add(s.nextInt());
}
*/
// Converting List to Array
Integer[] a = list.toArray(new Integer[list.size()]);
// Initialising object of Main class
GFG elem = new GFG();
// Finding sum of elements in array
// via add() method using recursion
int x = elem.add(a, a.length, 0);
// Print the sum of array/elements initially in List
System.out.println("Sum of elements in List :" + x);
}
// add() method to add elements in array
// using recursion
int add(Integer arr[], int n, int i)
{
if(i < n)
{
return arr[i] + add(arr, n, ++i);
}
else
{
return 0;
}
}
}
OutputElements in List : [10, 90, 30, 40, 70, 100, 0]
Sum of elements in List :340
Time complexity: O(n) where n is size of given list of numbers
Auxiliary space: O(n) because using extra space
Similar Reads
Java Program to Compute the Sum of Numbers in a List Using For-Loop
Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List
2 min read
Java Program to Compute the Sum of Numbers in a List Using While-Loop
The task is to compute the sum of numbers in a list using while loop. The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion
2 min read
Java Program to Find Sum of N Numbers Using Recursion
Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Assume/Identify the smaller problem from the problem which is similar to the bigger/ori
4 min read
Java Program to Find Reverse of a Number Using Recursion
Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Step 1: Assume the smaller problem from the problem that is similar to the bigger/origi
5 min read
Java Program to Find the Sum of First N Odd & Even Numbers
When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve
3 min read
Java Program to Find Sum of Natural Numbers Using While Loop
While loop arises into play where beforehand there is no conclusive evidence that how many times a loop is to be executed. This is the primary reason as there is no strict tight bound over how many numbers the sum is to be evaluated. Situations in which the output can be displayed using test conditi
3 min read
Java Program To Add Two Numbers Represented By Linked Lists- Set 1
Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input:Â List1: 5->6->3 // represents number 563Â List2: 8->4->2 // represents number 842Â Output:Â Resultant list: 1->4->0->5 // re
4 min read
Java Program For Adding 1 To A Number Represented As Linked List
Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to
6 min read
Java Program For Adding Two Numbers Represented By Linked Lists- Set 2
Given two numbers represented by two linked lists, write a function that returns the sum list. The sum list is linked list representation of the addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion). Example : Input:
8 min read
Java Program to Access the Part of List as List
A List is an ordered sequence of elements stored together to form a collection. A list can contain duplicate as well as null entries. A list allows us to perform index-based operations, that is additions, deletions, manipulations, and positional access. Java provides an in-built interface <<ja
4 min read