In: Computer Science
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList. For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18. The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined. The higher value is 18. If the character count is the same, you can return either value (which would be identical). Empty Arraylist character count would be 0.
public int biggerCharacterCount (ArrayList<String> list1, ArrayList<String> list2) {
}
/*
* Java method that takes as inputs 2 array list of
strings and returns
* the higher count of the characters from the two
ArrayList.
*/
public int biggerCharacterCount
(ArrayList<String> list1, ArrayList<String> list2)
{
// initialize total number of
characters in each array list to 0
int lengthList1 = 0, lengthList2 =
0;
// loop over list1, adding the
length of each String to lengthList1
for(int
i=0;i<list1.size();i++)
lengthList1 +=
list1.get(i).length();
// loop over list2, adding the
length of each String to lengthList2
for(int
i=0;i<list2.size();i++)
lengthList2 +=
list2.get(i).length();
// if lengthList1 >=
lengthList2, return lengthList1
if(lengthList1 >=
lengthList2)
return
lengthList1;
else
// else
return lengthList2
return
lengthList2;
}