0

I want to make a for-loop to add a letter to each string object in my list. I'm just not sure how to edit the objects in the list and not the actual list.

for instance, if I wanted to add "ing" to the end of each object in my list..

I feel like it's something simple, but I've been looking through oracle forever and haven't been able to figure it out if anyone can point me in the right direction?

I could use any kind of list really.. just anything that works.

I was thinking something like,

    String[] stringArray = tools.toArray(new String[0]);
            for (int i = 0; i < stringArray.length; i++)
            {
                stringArray[i] = stringArray[i].*part that would modify would go here*
            }
2
  • 1
    You can't modify Strings, so you need to use some other kind of object or modify list. Commented Sep 25, 2016 at 23:17
  • updated answer. Commented Sep 25, 2016 at 23:27

5 Answers 5

1

You cannot edit a String. They are immutable. However, you can replace the entry in the list.

ArrayList<String> list = new ArrayList<>();
list.add("load");
list.add("pull");

for (int i = 0; i < list.size(); ++i) {
    list.set(i, list.get(i) + "ing");

You updated your question to specify a static array:

stringArray[i] = stringArray[i] + "ing";

The right side of the assignment is performing a String concatenation which can be done with the + operator in Java.

Sign up to request clarification or add additional context in comments.

1 Comment

I think doing stringArray[i] = stringArray[i] + "ing"; will automatically replace the old reference with the one of the new String created for the concatenation.
1

You can use StringBuilder for this purpose.

public static void addIng(String[] arr) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < arr.length; i++) {
        sb.setLength(0);
        sb.append(arr[i] + "ing");
        arr[i] = sb.toString();
    }
}

Comments

0

Strings are immutable in java; they can't be modified once created.

Here it seems you have a few options; you can create a method that takes your list and returns a new list, by appending 'ing' to the end of each string in the list.

Alternatively, if you need to keep a reference to the original list, you can loop over the contents of the list (ArrayList?) and pop each string out, create a new string with the appended 'ing', and replace in the list.

Comments

0

Something like

List<String> list = new ArrayList<>();
list.add("testing");
for(String s:list){
    s=s+"ing";
}

Comments

0

Please take a look below samples.

   //Java 8 code.
   List<String> oldList = Arrays.asList("a", "b");
   List<String> newList = oldList.stream().map(str -> new StringBuilder(str).append("ing").toString()).collect(Collectors.toList());
   System.out.println(oldList); // [a, b]
   System.out.println(newList); // [aing, bing]

   // Java 7 or below.
   List<String> oldList = Arrays.asList("a", "b");
   List<String> newList = new LinkedList<String>();
   for (String str : oldList) {
       newList.add(new StringBuilder(str).append("ing").toString());
   }
   System.out.println(oldList); // [a, b]
   System.out.println(newList); // [aing, bing]

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.