2

Assume I have an ArrayList of Strings that holds the words, "hello" and "world". I want to add the word "java" to the end of each.

I have tried to do it while looping, but did not succeed. Please suggest me a method for the same. The other way, I found was to create a different ArrayList and copy contents, however I don't think is efficient in terms of space.

import java.util.ArrayList;

public class EditArrayListInLoop {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();

        arrayList.add("hello");
        arrayList.add("world");

        /* This does not work */
        /*for(int i = 0;i<arrayList.size();i++)
        {
            arrayList.get(i) += "java";
        }*/

        ArrayList<String> arrayList2 = new ArrayList<String>();
        for(int i = 0;i<arrayList.size();i++)
        {
            String test = arrayList.get(i);
            test += "java";
            arrayList2.add(test);
        }
        System.out.println(arrayList2);
    }
}   
saltandwater
  • 791
  • 2
  • 9
  • 25
  • Also refer http://stackoverflow.com/questions/4352885/how-do-i-update-the-element-at-a-certain-position-in-an-arraylist – Unknown Sep 08 '16 at 06:37

4 Answers4

9

test += "java"; doesn't change the content of the String returned by arrayList.get(i). It creates a new String. Strings are immutable, so there isn't any way to change the String objects within the List. You can only replace them by new String objects.

Use arrayList.set(index,newValue) to replace the i'th element of the List:

for(int i = 0;i<arrayList.size();i++)
{
    arrayList.set(i,arrayList.get(i)+"java");
}
Eran
  • 387,369
  • 54
  • 702
  • 768
4

I think you should manipulate the first list only. Using an another list is not an optimal solution.

Here's the code.

Output

[hellojava, worldjava]

Code

import java.util.ArrayList;

public class EditArrayListInLoop {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();

        arrayList.add("hello");
        arrayList.add("world");

        for(int i = 0;i<arrayList.size();i++)
            arrayList.set(i, arrayList.get(i).concat("java"));

        System.out.println(arrayList);
    }
}   

Please note that Strings are immutable. Whenever you change the content of String, you're not actually appending anything behind the scenes. You are creating a completely new String.

If the contents of your Strings are expected to change, then the advisable way is to use the StringBuilder class instead:

Documentation

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

Here's the code:

import java.util.ArrayList;

public class EditArrayListInLoop {
    public static void main(String[] args) {
        ArrayList<StringBuilder> arrayList = new ArrayList<StringBuilder>();

        arrayList.add(new StringBuilder("hello"));
        arrayList.add(new StringBuilder("world"));

        for(int i = 0;i<arrayList.size();i++)
            arrayList.set(i, arrayList.get(i).append("java"));

        System.out.println(arrayList);
    }
}   

P.S.: If such synchronization is required then it is recommended that StringBuffer be used.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
2

try using the set method.

    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("hello");
    arrayList.add("world");

    for(int i = 0;i<arrayList.size();i++)
    {
        String str = arrayList.get(i);
        arrayList.set(i, str + " java");
    }

    for(int i = 0;i<arrayList.size();i++)
    {
        String str = arrayList.get(i);
        System.out.println(str);
    }   
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

You are looking for:

arrayList.set(i, arrayList.get(i) + "java");

More info on ArrayList: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

js441
  • 1,134
  • 8
  • 16