Create the Java function to receive an ArrayList object The
Create the Java function to receive an ArrayList object. The function will count all of the single-digit positive numbers in the array and return that total.
Solution
CountSingleDigit.java
import java.util.ArrayList;
public class CountSingleDigit {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(11);
list.add(112);
list.add(1);
list.add(1441);
list.add(1155);
list.add(6);
list.add(81);
list.add(8);
list.add(3);
list.add(111111);
int count = getSinglDigitCount(list);
System.out.println(\"Count = \"+count);
}
public static int getSinglDigitCount(ArrayList<Integer> list){
int count = 0;
for(int i=0; i<list.size(); i++){
if(list.get(i) >=0 && list.get(i) <= 9){
count++;
}
}
return count;
}
}
Output:
Count = 4
