Write a method called buildString that accepts an ArrayList of strings as a para
ID: 3570228 • Letter: W
Question
Write a method called buildString that accepts an ArrayList of strings as a parameter and return a String as the output. You should go through the ArrayList and extract every other word, concatenate it as a String and return that string. For example , if the list originally contains the string values of {"how","are","now?"} before the method was called then the String produced will be "how are you now?". Write a method called buildString that accepts an ArrayList of strings as a parameter and return a String as the output. You should go through the ArrayList and extract every other word, concatenate it as a String and return that string. For example , if the list originally contains the string values of {"how","are","now?"} before the method was called then the String produced will be "how are you now?".Explanation / Answer
import java.util.*;
class Test{
public static String buildString(ArrayList AL){
String str ="";
for(int i=0; i<AL.size(); i++){
if(i!=AL.size()-1)
str += AL.get(i) + " ";
else
str += AL.get(i);
}
return str;
}
public static void main(String[] args){
ArrayList AL = new ArrayList();
AL.add("how");
AL.add("are");
AL.add("you");
AL.add("now");
System.out.println(buildString(AL));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.