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 7741639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:04:36+00:00 2026-06-01T09:04:36+00:00

I don’t know either my implementation have bugs or StringBuilder is going wrong. From

  • 0

I don’t know either my implementation have bugs or StringBuilder is going wrong. From the stackoverflow, i got to know that Java Strings are immutable. Currently i am doing some string processing in a recursive fashion using StringBuilder. To make recursion more clear and understandable i am using a Queue to store StringBuilder. The same processing can be done with Strings but it will increase the execution time, so i am using StringBuilder. The input string is first converted to StringBuilder then added to Queue after that a for loop matches it character and replaces substring with a char. This resultant StringBUilder is added to the Queue. Then an element(StringBuilder) is removed from the Queue and processed with for loop in the same previous manner. This process is repeated until the Queue is not empty. Below is my code snippet-

    Queue<StringBuilder> q = new LinkedList<StringBuilder>();
    StringBuilder sb;
    q.add(new StringBuilder("abcccc"));
    while ((sb = q.poll()) != null) {
        int len = sb.length();

        for (int i = 0; i < len - 1; i++) {
            /*
             * Here i am matching charAt(i) with charAt(i+1) and based on
             * the characters matching decision is done
             */

            StringBuilder rep = new StringBuilder(len - 1);
            rep = sb;
            rep.replace(i, i + 2, replace);//replace is a string which is a character
            q.add(rep);
            }
    }   

This gives me StringIndexOutOfBoundsException. However i am not changing the content of original StringBuilder, which is sb. I am just creating a copy of sb which is rep, then replacing the content of this newly created StringBuilder.

  • 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-01T09:04:37+00:00Added an answer on June 1, 2026 at 9:04 am

    rep = sb is not creating a copy of sb. Both of those objects are now pointing to the same StringBuilder instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know why people do not practice AJAX implementation for authentication systems. Is it
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't know what's wrong here, when I run the application it says Specified method
Don't know where else to ask, but from one day to the other my
Don't know if it's I'm doing it wrong or if there's a bug (I
Don't worry, I'm not going to ask that question, yet again... I am wanting
DON'T ASK WHY but... I have a regex that needs to be case insensitive
Don't know if this has been answered before. Have custom routes to users. If
I don't have much PHP experience and I want to know how to best
Don't yell at me! i have seen many threads claiming that HTML cannot be

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.