Write a public instance method named Integer arrayListSum(ArrayList pList). The method shall return the sum of the elements of pList as an Integer. The method shall return 0 when: (1) the pList parameter variable is null; or (2) pList is the empty list.

Respuesta :

Answer:

Include the conditional case when pList is equal to zero.

Explanation:

// Initially it is necessary to import the libraries

import java.util.*;

import java.io.*;

public class H01_34 {

   public Integer arrayListSum(ArrayList<Integer> pList) {

       // You might have forgotten the possibility when the pList is zero.

       if (pList == null) {

       //once pList is null it will return 0 to the main            

       return 0;

       }

       int sum = 0;

       for (int i = 0; i < pList.size(); i++) {

           sum += pList.get(i);

       }

       return sum;

   }

}