175k views
1 vote
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.

User Newnab
by
5.3k points

1 Answer

2 votes

Answer:

Include the conditional case when pList is equal to zero.

Step-by-step 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;

}

}

User Asdfkjasdfjk
by
5.9k points