Write a method f that accepts an ArrayList containing String
Write a method f that accepts an ArrayList containing String objects . The method should return a String containing the first character of each string in the ArrayList, in the order in which they appear. Thus, if the ArrayList contains the Strings : Hello world goodbye the return value of the method would be the String Hwg (must be in JAVA)
Solution
ArrayListCheck.java
import java.util.ArrayList;
public class ArrayListCheck {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add(\"Hello\");
list.add(\"world\");
list.add(\"goodbye\");
String returnStr = stringFirstLetters(list);
System.out.println(\"The first character of each string in the ArrayList: \"+returnStr);
}
public static String stringFirstLetters(ArrayList<String> list){
String s = \"\";
for(String str: list){
s = s + str.charAt(0);
}
return s;
}
}
Output:
The first character of each string in the ArrayList: Hwg
