I'm trying to create a method that will take in the String on an ArrayList and then add text to the beginning of each of the string. Such as If I have an ArrayList of names {jon, jimmy, kyle}. I would want the method to place "Good Morning, " in the string before the name. so it would return "Good Morning, jon", Good Morning, jimmy", "Good Morning, kyle". I have Searched and found the append but it seems that it is for a array not an array list. I really can't even find a good starting point. Any help would be greatly appreciated. Thank you.
Code of really no use, but at least its something.
public adding(ArrayList<String> al)
{
StringBuilder us = new StringBuilder();
us.append("("+al[0]);
for(int i = 1; i < al.length;i++)
{
us.append("Good Morning, " + al[i]);
}
return us;
}
String are inmutable. So you have to set again in the list the value.
public void adding(ArrayList<String> al){
for(int i = 0; i < al.size();i++){
al.set(i,"Good Morning, "+al.get(i));
}
}
try this
public void adding(ArrayList<String> al)
{
for(int i = 0; i < al.size();i++)
{
al.set(i,"Good Morning , "+al.get(i));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With