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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:51:18+00:00 2026-06-04T19:51:18+00:00

Of course this is an impossible statement in java (to-date), however ideally I would

  • 0

Of course this is an impossible statement in java (to-date), however ideally I would like to implement it as it is at the heart of many iterations. For example the first multiple times it is called I’m doing it 650,000+ times when it is creating the ArrayList.
Unfortunately the reality is that my actual code does not have the set inside the else loop; thus it will pass over both the add and then the set commands and wasting time.

After that I have it also in another loop where it is only performing the set as the data is already created and this is multi-nested with in many others so it is a lengthy process.

ArrayList<Integer>  dataColLinker = new java.util.ArrayList<Integer>();
...
...
public void setLinkerAt( int value, int rowIndex) {
    ...
    while(rowIndex >= dataColLinker.size()) {
        dataColLinker.add(value);
    } else {
        dataColLinker.set(rowIndex, value);
    }

Any ideas or theories?
I’m unsure about speeds in java when it comes to if statements and ArrayList commands and so on

  • 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-04T19:51:19+00:00Added an answer on June 4, 2026 at 7:51 pm

    Am I missing something?

    Doesn’t this hypothetical code

    while(rowIndex >= dataColLinker.size()) {
        dataColLinker.add(value);
    } else {
        dataColLinker.set(rowIndex, value);
    }
    

    mean the same thing as this?

    while(rowIndex >= dataColLinker.size()) {
        dataColLinker.add(value);
    }
    dataColLinker.set(rowIndex, value);
    

    or this?

    if (rowIndex >= dataColLinker.size()) {
        do {
            dataColLinker.add(value);
        } while(rowIndex >= dataColLinker.size());
    } else {
        dataColLinker.set(rowIndex, value);
    }
    

    (The latter makes more sense … I guess). Either way, it is obvious that you can rewrite the loop so that the “else test” is not repeated inside the loop … as I have just done.


    FWIW, this is most likely a case of premature optimization. That is, you are probably wasting your time optimizing code that doesn’t need to be optimized:

    • For all you know, the JIT compiler’s optimizer may have already moved the code around so that the “else” part is no longer in the loop.

    • Even if it hasn’t, the chances are that the particular thing you are trying to optimize is not a significant bottleneck … even if it might be executed 600,000 times.

    My advice is to forget this problem for now. Get the program working. When it is working, decide if it runs fast enough. If it doesn’t then profile it, and use the profiler output to decide where it is worth spending your time optimizing.

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

Sidebar

Related Questions

As an exercise for my Java course in Uni this morning, I had to
I have a table that looks like this for about ~30 students: StudentID Course*
Is it possible to define a constraint like this: A <- SELECT Enrolment course
I just uncovered this confusion and would like a confirmation that it is what
Of course we can do this, but is it alright to do so? Are
(Came up with this question in the course of trying to answer this other
I'm doing the PluralSight JavaScript Fundamentals course and he enters this code into the
This one boggles my mind. I've used this same script (different targets of course)
Is there a way to say this method returns this using Generics? Of course,
I was doing a project for computer course on programming concepts. This project was

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.