In Java Define a method so that it returns a count of all th

In Java. Define a method so that it returns a count of all the elements of its argument, an ArrayList<String>, that have length 3. If the argument is null, return 0.

How to return 0, if ( ______ == null) ???

How to return count of all elements in the argument ArrayList<String> that have length 3 ???

Solution

Answer:

      public int countElements(ArrayList<String> list){
       if(list == null){
           return 0;
       }
       else{
           int count = 0;
           for(String s: list){
               if(s.length() == 3){
                   count++;
               }
           }
           return count;
       }
   }

In Java. Define a method so that it returns a count of all the elements of its argument, an ArrayList<String>, that have length 3. If the argument is null

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site