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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:21:53+00:00 2026-05-27T13:21:53+00:00

Looked around, couldn’t find any similar questions in java.. Basically I need to add

  • 0

Looked around, couldn’t find any similar questions in java..

Basically I need to add a number to an int array in a specific position index

I can only use Arrays, no ArrayLists

Here is what I have so far, and I know why it doesn’t work, but I can’t figure out how to fix that problem of overwriting, which I don’t want it to do.

The task is a non-overwriting insert. e.g. the final result would be

[1 2 1337 3 4 5 6 7 8]

Here is the code snippet:

public void main(String[] args)
{
int[] array = {1,2,3,4,5,6,7,8};
array = add(array, 2, 1337);
for(int i : array)
    System.out.print(i + " ");
}


public int[] add(int[] myArray, int pos, int n)
{
    for (int i = pos; i<myArray.length-1; i++){
        myArray[i] = myArray[i+1];
    }
    myArray[pos] = n;
    return myArray;
}
  • 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-27T13:21:53+00:00Added an answer on May 27, 2026 at 1:21 pm

    Your problem is this loop:

    for (int i = pos; i<myArray.length-1; i++){
        myArray[i] = myArray[i+1];
    }
    

    It is writing i+1 into i – ie it moves element down – you need it to move them up. In order to move up, you need to iterate down (otherwise you overwrite what you just wrote).
    Try this:

    for (int i = myArray.length - 1; i > pos; i--) {
        myArray[i] = myArray[i - 1];
    }
    

    Note that this will make room for the insertion at pos by losing (overwriting) the last element.

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

Sidebar

Related Questions

I've looked around on SO but I couldn't find any similar question. After opening
I've looked around the current answers for similar questions but still couldn't find the
I have looked around for similar questions and I couldn't find an answer... I'd
I've looked around but I couldn't find any answers. Anyway, as the title says
I've already looked around but couldn't find the exact solution/problem I'm having right now.
Im facing some problems, I looked around in the forum and didnt find any
I looked around and couldn't find one. Is there an equivalent of Log4X for
Looked around, couldn't find this specific question discussed. Pretty sure the difference is negligible,
I've looked around for this but I couldn't find an answer, and I don't
I've looked around for an answer to this one but couldn't find one. I

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.