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

  • Home
  • SEARCH
  • 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 6940501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:45:38+00:00 2026-05-27T12:45:38+00:00

For this for loop, is the run time O(n) or O(n^2): char[] ar =

  • 0

For this for loop, is the run time O(n) or O(n^2):

char[] ar = new char[1000];
String s = "";
Arrays.fill(ar, 'a');
for(Character c: ar){
    s += c;
}

So basically, what is the run time of + on a String? How does it work behind the scene in Java?

  • 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-05-27T12:45:39+00:00Added an answer on May 27, 2026 at 12:45 pm

    Java strings are immutable. Every time you do:

    s+=c;

    You’re really saying:

    s = new String(s + c);

    new String(s + c) must allocate a string of length s + 1, or:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    …
    etc.

    Since Sum(1..N) == (n + 1) (n / 2), this is O(n^2).

    One of the cases where StringBuilder is a definite advantage.

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

Sidebar

Related Questions

here i want to run this program and take one character pointer like char
I'm trying to run this for loop; for (int col= 0; grid[0].length; col++) However
When I run this code, About half-way through the concatenation loop, $xml becomes null
I've run into this while writing a Traveling Salesman program. For an inner loop,
Basically i got this for loop and i want the number inputed (eg. 123)
I have this code: void fill_array (unsigned int *iarray, char *string, int max) {
I'm new to VBA and I'm trying this for loop out, but I think
I have this loop while (count($arr) < 7) { $string = 'xx'; for ($i=0;
This loop checks if a record is in the sqlite database and builds a
Why was this loop introduced in java?Is it a java creation? What is its

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.