Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find & replace sing. char. in an ArrayList<String> element

I am interested in iterating through (re: find and replace purposes), say:

List<String> someList = new ArrayList<String>();

where someList is already populated in an earlier method, and consists of, say just a couple elements, in the fashion of, call it [a:bX, b:Xc],

where the find-and-replace String(s) of interest are, say:

String someString = "X";
String otherString = "Y";
String contentsTBD = "";

Now, ideally I thought I could've iterated over someList like so:

public void readAndReplace() {
    for (int i = 0; i < someList.size(); i++) {
        if (someList.get(i).contains(someString)) {
            someList.get(i).replace(someString, otherString);
        } else {
            ++i;
        }
    }
    System.out.print(someList);
}

wherein the printout should read:

[a:bY, b:Yc]  

Then, I thought this might work:

public void readAndReplace() {
    for (String s : someList) {
        contentsTBD += s;
    }
    for (int i = 0; i < contentsTBD.length(); i++) {
        if (contentsTBD.contains(someString)) {
            contentsTBD.replaceAll(someString, otherString);
        } else {
            ++i;
        }
    }
    System.out.print(contentsTBD);
}

but then quickly realized that this was nonsensical since my reference to i was lost. Any advice would be really helpful. Thank you.

like image 982
OcelotXL Avatar asked Oct 12 '12 05:10

OcelotXL


People also ask

What means of find?

Definition of find (Entry 1 of 2) transitive verb. 1a : to come upon often accidentally : encounter found a $10 bill on the ground. b : to meet with (a particular reception) hoped to find favor. 2a : to come upon by searching or effort must find a suitable person for the job.

What word class is find?

As detailed above, 'find' can be a verb or a noun. Verb usage: Project Gutenberg finds that Find is the 190th most important word in the English language.

Where is my phone location?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.


Video Answer


2 Answers

  • First, you are not storing your Replaced String anywhere. It is gone with the wind.

  • Second, your replace will not modify the existing list. You would need to set the new string into the existing location, since you are using traditional for-loop. Or, you can have a new list, and add modified values to that list.

  • Remember, since String in Java is immutable, so all the methods of String class return a new string. They do not modify the existing one. So, you need to re-assign the returned String into a new one.

Try out this code: -

 public void readAndReplace()
    {
      // You can also create a new list out of the existing list.
      // That way, you won't need to modify the existing one.
      List<String> newList = new ArrayList<String>();
      for(int i = 0; i < someList .size(); i++)
      {
          if(someList.get(i).contains(someString))
          {
              newList.add(someList.get(i).replace(someString, otherString));
             //someList.set(i, someList.get(i).replace(someString, otherString));
          } else {

              // If it not contains `someString`, add it as it is to newList
              newList.add(someList.get(i));
          }

       }
       System.out.println(someList);  // Original
       System.out.println(newList);   // New List

    }
like image 52
Rohit Jain Avatar answered Nov 04 '22 17:11

Rohit Jain


Edited added explanation,after the suggestion of MadProgrammer,

Point 1: String is Immutable, and you are trying to modify the string with someList.get(i).replace(someString, otherString); that will work, but not get reflected inside your someList, to reflect your someList you have to call the someList.set(i)

Point 2: Your else block is useless as you already incrementing the i inside the for loop

try this.

  String oldStr="";
    for (int i = 0; i < someList.size(); i++) {
        if (someList.get(i).contains(someString)) {
            oldStr = someList.get(i).replace(someString, otherString);
            someList.set(i, oldStr);
        } 
    }
    System.out.print(someList);

see how Immutable work in java immutable

like image 28
subodh Avatar answered Nov 04 '22 17:11

subodh