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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:03:37+00:00 2026-06-12T07:03:37+00:00

I have a method, seemingly fine, for putting double values into a double array.

  • 0

I have a method, seemingly fine, for putting double values into a double array. It is

insert(int i, double value)

where i is the index (array[i]) and value is what I want in that index.

I split the method into edge cases, built in a sufficient safe block of initialized array space (length) and buffered that with a part of the method that doubles the length every time the number of elements is equal or greater than the length. I then put methods for when the input i is greater than the number of items (numItems) of the array and when it is less than numItems. i < numItems is working fine, but when I try to put in

insert(63,3)
insert(15,3)
insert(23,3)

into my (1,-1,5,23) array I only get 2 three’s on the last part of my array. My initial array length is 10, so it’s not a memory issue. I thought it might be a print method error and tried to obtain the last element manually, which told me the index was empty. Thus it is a logic error in my method, which follows.

// if i is greater than the number of items, insert value into numItems index,
// and not any farther. e.g. if i = 100000 and numItems = 10, put value into
// items[10] and not items[100000];
if (i > numItems) 
{
    items[numItems] = value; 
    numItems++; //add to counter
    return;
}

The thing is, it’s such simple code that I can’t tell what’s wrong with it. Very intuitive, and very puzzling. Ideas?

below is the whole of the insert method

public void insert(int i, double value) //insert value into array[i]
{
    if(i < 0)
    {
        System.out.println("i < 0; please input i >= 0 for array indices."); //an array cannot have an indice < 0;
        return;
    }

    if (numItems >= items.length) // if the number of items becomes equal or greater than the array containing it
    {
        double[] tempItems = new double [items.length * 2]; // create a new array double the size of current
        for(int j =0 ; j < items.length; j++ ) //and copy all elements into the new array
        {
            tempItems[j] = items[j];
        }

        items = tempItems; //set the temp array as the main array.
    }

    if (i > numItems) //if i is greater than the number of items, insert value into numItems index, and not any farther.
    {                 // i.e. if i = 100000 and numItems = 10, put value into items[10] and not items[100000];
        items[numItems] = value; 
        numItems++; //add to counter
        return;
    }

    if ( i < numItems) //if i is inside the used boundaries of the array
    {
        for (int k = numItems; k > i; k--) //shift values over to the right.
        {   
            items[k]=items[k-1];
        }

        items[i] = value; //and insert value into i
        numItems++; //add to counter
        return;
    }


}
  • 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-12T07:03:39+00:00Added an answer on June 12, 2026 at 7:03 am

    In case of any modifications that alter array size (insert or delete) it is recommended to use java.util.List implementations. ArrayList, for example. It saves you from headache with temporary arrays and moving of elements.

    Also, to copy some elements from array to array you should consider using existing methods like System.arraycopy and various copying methods from java.util.Arrays.

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

Sidebar

Related Questions

I have some Core Data functionality that was working fine until some recent (seemingly
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method that returns module path of given class name def findModulePath(path, className):
I have method in a class that I need to make sure is only
I've got some 3rd party beans that have method signatures that fit quite well
I have a method(say method1) that writes to database(sqlserver)and another method(say method2) that tries
A seemingly straightforward problem: I have a java.util.concurrent.Semaphore , and I want to acquire
Seemingly simple concept but can't get past this. I have a Command...the _Executed method
The following method loads data from an array into custom cells of UITableView. The

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.