Results 1 to 3 of 3
  1. #1
    Zaibatsu's Avatar
    Zaibatsu is offline Member
    Join Date
    Dec 2011
    Netherlands, the
    Rep Power

    Default Shifting an Array

    Hello everyone,

    I started learning Java a few months ago online and using the book "Java: A Complete Reference" and "Java for Dummies". I am a beginner and having a hard time learning Java (I only know some basics of HTML and PHP).

    At this moment I am trying to shift an array one value to the right.

    I managed (with the help of a friend) to get this code:

    Java Code:
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
    package pprem4;
    public class PPRem4 {
         * @param args the command line arguments
        public static void main(String[] args) {
            // TODO code application logic here
            int array[] = {0,1,2,3,4};
            int amount = 1;
            shiftRight(array, amount);
        public static void shiftRight(int[] array, int amount) {
            for (int j = 0; j < amount; j++) {
                int a = array[array.length - 1];
                int i;
                for (i = array.length - 1; i > 0; i--) {
                    array[i] = array[i - 1];
                            System.out.println(i + " " +array[i]);
                array[i] = a;
    How do I move the last element to become the first element in the array? I'd like an easy solution since I am a beginner. I also would like, if possible, a brief explanation of what the code I am asking for does. I'm not sure if it is important to mention but I use NetBeans to learn Java.

    Thank you very much.
    Last edited by DarrylBurke; 12-13-2011 at 06:16 PM. Reason: Changed QUOTE tags to CODE tags

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power

    Default Re: Shifting an Array

    Quote Originally Posted by Zaibatsu View Post
    How do I move the last element to become the first element in the array?
    Store the last element in a temporary variable. Move all elements to the right. Store temporary variable in the first location of the array.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    London, UK
    Blog Entries
    Rep Power

    Default Re: Shifting an Array

    make your code more readable so that you don't confuse yourself with unhelpful variable names

    this may not be the best way but here is a solution for you

    Java Code:
    public static void shiftRight(int[] intArray, int placesMoved) {
        /** move last int of intArray to the front
         *   we want to repeat this action 'placesMoved' number of times
         *   hence placesMoved limit on outer loop */
        for (int i=0; i<placesMoved; i++) {
            //code to take last element of an array and put it to the front
            int lastItem = intArray[intArray.length-1]
            //1. create a new int array initialized with the same size as the old one
            //2. copy the last element i.e. the variable 'lastItem' to the first index in the new array
            //3. create an inner loop to copy the rest of the items (not the one you copied already)
            for (int j=0; j<intArray.length-1; j++) {
                //position 0 in the new array is already occupied with the last element
                int k = j+1;
                //4. copy the next item to the next position in the new array
    hope this helps

Similar Threads

  1. Problem with shifting array elements
    By holy.water in forum New To Java
    Replies: 4
    Last Post: 02-25-2011, 09:56 PM
  2. Shifting method using >>
    By ile4 in forum New To Java
    Replies: 7
    Last Post: 11-09-2010, 07:19 PM
  3. Q about shifting data in an array
    By alihht in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 07:17 AM
  4. Shifting characters in array
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 10:19 PM
  5. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 07:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts