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

The Archive Base Latest Questions

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

Here is my array: int[] myArray = {0, 1, 2, 3, 4, 5, 6,

  • 0

Here is my array:

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

Let’s say I want to move myArray[3] (it could be any element) and myArray[6] (same with this one) to the front of the array while rearranging the back, how can I do this? Example:

This:

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

Into this:

{3, 6, 0, 1, 2, 4, 5, 7, 8, 9}
  • 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:46:12+00:00Added an answer on May 27, 2026 at 4:46 pm

    To move index x to the front, you need to:

    • Remember what’s at index x
    • Copy everything from 0 to x - 1 up one index, e.g. with System.arrayCopy
    • Set the value at index 0 to be the value you remembered in the first step

    For example:

    public void moveToHead(int[] values, int index)
    {
        // TODO: Argument validation
        int value = values[index];
        System.arraycopy(values, 0, values, 1, index - 1);
        values[0] = value;
    }
    

    Note that System.arraycopy handles the copying appropriately:

    If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.

    Your original example mentioned two elements – while you could potentially do all of this more efficiently knowing both elements in advance, it would be far, far simpler to model this as two moveToHead calls. You need to take care about the ordering – for example, if you want to move index 6 to the head first, you’ll need to then move index 4 rather than index 3, to take account of the first move.

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

Sidebar

Related Questions

here's the stored procedure i wrote.In this proc p_subjectid is an array of numbers
Simple question here: is there any way to convert from a jagged array to
Here is the vardump for my array: array(4) { [3]=> array(1) { [match]=> int(33)
I've got some problems/misunderstandings with arrays in C++. int myArray[30]; myArray[1]=2; myArray[2]=4; This is
Here's my code: int myArray[]={1,2,3,4,5,6,7,8}; for(int counter=myArray.length; counter > 0;counter--){ System.out.println(myArray[counter]); } I'd like
This is how I am declaring my array of integers: int registers[16]; And this
Here's my array: [2555] => Array ( [0] => stdClass Object ( [meta_id] =>
Here is the code: $arraya = array('a','b','c'); foreach($arraya as $key=>$value) { if($value == 'b')
Here is my code: - (void)loadView { //hard coded array of content for each
Here is pseudo-code of how I setup an array representing the MandelBrot set, yet

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.