Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding text to a string in an array list

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;
    } 
like image 379
Sabbathlives Avatar asked Nov 26 '25 06:11

Sabbathlives


2 Answers

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));
        }
}
like image 169
nachokk Avatar answered Nov 28 '25 19:11

nachokk


try this

public void adding(ArrayList<String> al)
{
       for(int i = 0; i < al.size();i++) 
       {
          al.set(i,"Good Morning , "+al.get(i));
       }
}
like image 37
Prabhaker A Avatar answered Nov 28 '25 20:11

Prabhaker A