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
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static int sumOfArray(Integer[] a, int n)
{
if (n == 0 )
return a[n];
else
return a[n] + sumOfArray(a, n - 1 );
}
public static void main(String[] args)
{
List<Integer> al = new ArrayList<Integer>();
al.add( 1 );
al.add( 2 );
al.add( 3 );
al.add( 4 );
al.add( 5 );
Integer a[] = new Integer[al.size()];
al.toArray(a);
System.out.print( "Elements in List : " );
for (Integer obj : a) {
System.out.print(obj + " " );
}
int sum = sumOfArray(a, a.length - 1 );
System.out.println();
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
import java.util.*;
public class GFG
{
int sum = 0 , j = 0 ;
public static void main(String[]args)
{
List < Integer > list = new ArrayList < Integer > ();
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);
Integer[] a = list.toArray( new Integer[list.size()]);
GFG elem = new GFG();
int x = elem.add(a, a.length, 0 );
System.out.println( "Sum of elements in List :" + x);
}
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