Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:52:35+00:00 2026-06-12T21:52:35+00:00

I am interested in iterating through (re: find and replace purposes), say: List<String> someList

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:52:37+00:00Added an answer on June 12, 2026 at 9:52 pm
    • 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
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in comparing multiple lists, taking the difference and iterating through that. Both
So I've been living in the dark ages of iterating through my data and
Interested in upgrading JAVA VERSION from JAVA 1.5 to JAVA 1.6 [oracle@server301 /]$ java
I interested to work with data types and file formats. For example I want
im interested how many api calls per second or per minute i can do
I am interested in doing this C code in Java: // sets n's ith
As i am Interested in artificial intelligence, I recently decided to give Lisp a
I'm interested in making ghost preview elements of very complex DOM objects. While I
I am interested in knowing about what other teams are doing about limiting internal
I am interested in theoretical Big-O analysis of the following MySQL query: SELECT id,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.