3

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;
    } 
3
  • Strings are inmutable, so you have to add again to the list and remove previous String xD Commented Sep 19, 2013 at 14:24
  • Also, why are you looping from i=1 in the for loop? it should be i=0 Commented Sep 19, 2013 at 14:29
  • It's worth noting that your design decision itself may be flawed. Why do you need to store "Good Morning, " before their names? It seems better to just hold the names in the AL(the unique part), and whenever you want to print with Good Morning, in front, then concatenate with the String at that point. Just my 2 cents though. Commented Sep 19, 2013 at 14:43

4 Answers 4

3

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));
        }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Then why use StringBuilder at all?
the method set is not applicable for the arguments. Also when returning the method I would return al correct?
Actually, the method doesn't have to return anything. The ArrayList that you passed in will get changed in place.
@Sabbathlives why set() is not applicable for the arguments..?It is applicable according to docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set
2

try this

public void adding(ArrayList<String> al)
{
       for(int i = 0; i < al.size();i++) 
       {
          al.set(i,"Good Morning , "+al.get(i));
       }
}

4 Comments

Much more straight forward than the StringBuilder solution
@Cruncher more straightforward but not as efficient. Still, in most cases this is what I would run with
@StormeHawke creating a new string builder to append to twice is faster than concatenating a String literal with another String?
@StormeHawke :), yeah I was just thinking. If creating a new StringBuilder and appending to it twice was faster than concatenating Strings, then the compiler would just use a StringBuilder everywhere you concatenate. But to be blunt, I don't know how concatenation is implemented exactly, or why appending 10 times to a StringBuilder is faster than 10 concatenates. I just know the Use Cases of when to use StringBuilder and when not to
0

See this

http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Joiner.html#appendTo(A, java.lang.Iterable)

2 Comments

How to answer: A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.
Thanks BackSlash for your valuable comment. I will follow that
0

You can use Apache Commons Lang that has a StringUtils class. It has a join function which joins arrays together to make a String.

Example:

String result="Good morning ";
result +=StringUtils.join(new String[] {"jon", "jimmy", "kyle"}, ", Good morning ");

The join() function accepts Iterable object and so it works for list too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.