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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:07:30+00:00 2026-05-27T16:07:30+00:00

Lets say I have the array: int[] taco = {0, 1, 2, 3, 4,

  • 0

Lets say I have the array:

int[] taco = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

How can I move an element based its index to the front? Example:

Move element taco[5] to the front should produce this:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
// becomes
{5, 0, 1, 2, 3, 4, 6, 7, 8, 9}

EDIT:
Does it make a difference if the ints are instead objects?

  • 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-27T16:07:30+00:00Added an answer on May 27, 2026 at 4:07 pm
    1. Store the value you’re moving in a temporary int variable
    2. Copy all elements down the array, one at a time, in a loop
    3. Assign the value you stored in the temporary variable to the new position (the front of the array, taco[0], in this case)

    It does not make any difference if it’s an array of int values, or objects. The algorithm is the same. Since an array of Object references would literally be an array of 32-bit or 64-bit memory references, you’re effectively still copying integers around.

    Here is code that would do it – there are several ways to do this, but this is the basic idea:

    public int[] moveValueAtIndexToFront(int[] arrayToBeShifted, int index) {
      int valueBeingMoved = originalArray[index];
    
      for (int i = index; i > 0; i--) {
        arrayToBeShifted[i] = arrayToBeShifted[i-1];
      }
    
      arrayToBeShifted[0] = valueBeingMoved;
    
      return arrayToBeShifted;
    }
    
    • This will moves all values at the specified index backward in the array by one position, and places the value being moved at the front.
    • If the index passed in is 0, then nothing winds up being moved.
    • If the index passed in happens to be the the index of the very last item in the array, every single item in the array needs to be shifted. If you’re dealing with large arrays, and you’re doing a lot of shifts of values late in the array, this becomes really inefficient, really fast.

    You could also take a look at the source code of arraycopy if you’re curious, via the OpenJDK project.

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

Sidebar

Related Questions

Lets say I have an array as: int a[]={4,5,7,10,2,3,6} when I access an element,
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};
Lets say I have an array numbers that contains the following values: int numbers
Lets say I have an array like int arr[10][10]; Now i want to initialize
Lets say I have a array defined in Groovy like this def int[] a
Let's say I have a C# variable and array: int variable_1 = 1; int[3]
Lets say I have an array like this: string [] Filelist = ... I
Lets say I have an array which has n dimensions. Now in order to
Lets say I have an array of length n and I want to pick
Lets say I have an array with a structure like this: $arr= Array( array(

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.