1

I don't know what's the problem in my code.Whatever I try to replace, not wroking. I have a private static ArrayList<String> lista.

I fill this. Then later in another method, whatever I try, I can't replace anything, like this:

public static void replacing() {
    Collections.replaceAll(lista, "a", "!!!!!!!!!!!!!!!!!!!!!");
}

Then I print this in the method and lista is the same, nothing changed. What should I check after?

public class MyProgram {
    private static ArrayList < String > lista;
    public static void fileReading() {
        lista = new ArrayList < String > ();
        try {
            inp = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(inFileNev), "ISO8859-1")));
            String sor;
            while ((sor = inp.readLine()) != null) {
                lista.add(sor);
                lista.add(System.getProperty("line.separator"));
            }
            inp.close();
        } catch...
    }
    public static void searching() {
        Collections.replaceAll(lista, "a", "b");
        System.out.println(lista.toString());
    }
}
11
  • 3
    You should consider to edit your question and add your lista contents since your snippet looks correct Nov 6, 2012 at 18:05
  • what does lista contain and how did you create it?
    – Ankur
    Nov 6, 2012 at 18:05
  • 5
    Please be specific. What exactly do you put into the list? Post the complete code that creates a list, adds into it, and tries to call your method. Nov 6, 2012 at 18:06
  • does your lista contains an element/String "a"????
    – Ankur
    Nov 6, 2012 at 18:06
  • 1
    Is it possible that you are trying to replace any character 'a' in any of the elements in your ArrayList with the string "!!!!!!!!!!!!!!!!!!!!!" ??
    – Amar
    Nov 6, 2012 at 18:12

3 Answers 3

11
List<String> list = Arrays.asList(new String[] {"a","b"});      
System.out.println(list);
Collections.replaceAll(list, "a", "!!!!!");
System.out.println(list);

gives

[a, b]
[!!!!!, b]

The above code sample shows that Collections.replaceAll indeed works.

0
2

This is an example explaining Collections.replaceAll

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> sLst = new ArrayList<String>();
        sLst.add("A");
        sLst.add("B");
        sLst.add("C");
        sLst.add("A");
        // This will replace all "A" with "Z"
        Collections.replaceAll(sLst, "A", "Z");
        System.out.println(sLst);// [Z, B, C, Z]

    }
}
2
List<String> list = Arrays.asList(new String[] {"a","ba",new String("a")});      
Collections.replaceAll(list, "a", "!!!!!");
System.out.println(list);

output would be

!!!!! ba !!!!!

it would not replace 'a' in "ba"

3
  • Thank you all. I can only give the relevant code in 2 answers: public class MyProgram { private static ArrayList<String> lista; public static void fileReading() { lista = new ArrayList<String>(); try { inp = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(inFileNev), "ISO8859-1"))); String sor; while ((sor = inp.readLine()) != null) { lista.add(sor); lista.add(System.getProperty("line.separator")); }
    – AllainLG
    Nov 6, 2012 at 18:43
  • inp.close(); } catch... } public static void searching() { Collections.replaceAll(lista, "a", "b"); System.out.println(lista.toString()); } } I tried this, but it only prints the original lista It contains several 'a' and tried with others, but it replaced nothing.
    – AllainLG
    Nov 6, 2012 at 18:45
  • I'm not sure is this the right method I'm looking for. I need the replace old string like: abc(... or abc ( , where I'd like to replace "abc" to a new string like "xyz". "abc" is a method name, so I must search for '(' too, and replace just the name, and leave what's after Would replaceAll() work fine for this?
    – AllainLG
    Nov 6, 2012 at 19:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.