user3653759
user3653759

Reputation:

Shifting elements in array

I am trying to shift elements in an array. I tried from the book but it does not seem to work. The user types in how many to shift the array by in the main class, and then it gets sent into a shifter class into a shift method. Starting from array position [1].

This is what I have:

// Pos is the users entered for how many positions to shift
// data is the array of elements 1-length of data
// temp is place holder

public void shift(int pos)
{
    while(pos > 0)
    {
        int temp = data[data.length];

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

Upvotes: 0

Views: 13059

Answers (6)

Am_I_Helpful
Am_I_Helpful

Reputation: 19178

The reason why you are getting ArrayIndexOutOfBoundsException is that your data array has a size of data.length (counting from 1), but you have tried to access the data[data.length] element in the last iteration of the loop which is data.length+1 element of the array which doesn't exist and is out of bound of array, because of array index starting from 0 in Java.

Correct Code :-

// within your while loop
int temp=data[data.length-1];
for(int i=data.length-1;i>=1;i--)
{
  data[i] = data[i-1];
}
// decrease the position now
 data[0]=temp;

Kindly check the below link to know more about this Exception :-

ArrayIndexOutOfBoundsException---How to Handle

Upvotes: 0

lukaspp
lukaspp

Reputation: 1149

Sorry for the late answer, but I think the easiest way is to se module like this:

int[] original = { 1, 2, 3, 4, 5, 6 };
int[] reordered = new int[original.length];
int shift = 1;

for(int i=0; i<original.length;i++)
    reordered[i] = original[(shift+i)%original.length];

Upvotes: 0

Ege Kuzubasioglu
Ege Kuzubasioglu

Reputation: 6282

Probably the easiest solution:

private static void shiftArray(int[] array) {

    for (int i = 0; i < array.length - 1; i++) {

      int temp = array[i];
      array[i] = array[i + 1];
      array[i + 1] = temp;
    }

    for (int p : array)
      System.out.print(p +  " ");
  }
}

Upvotes: 1

user3475136
user3475136

Reputation: 81

int temp=data[data.length-1];
for(int i=data.length-1;i>=1;i--)
{
data[i+1] = data[i];
}
data[0]=temp;

Upvotes: 1

gp.
gp.

Reputation: 8225

if you want to shift right and circularly, from 1 to end by pos positions this is what you can do:

public void shift(int pos) {

    while (pos-- > 0) {//shift 1 right for pos number of times

        //notice the data.length-1 for the last item. data.length as index would be out of bound.
        int tmp = data[data.length - 1];

        //start from the last and keep shifting the left one to current.
        for (int i = data.length - 1; i > 1; i--) {
            data[i] = data[i - 1];
        }

        //since it's a circular shift, last one (before shifting) will shift to 1 index.
        data[1] = tmp;
    }
}

Upvotes: 0

zgc7009
zgc7009

Reputation: 3389

Put it in a while loop with some sort of break command, in this example "quit"

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);  

boolean run = true;
while(run){
    System.out.println("What would you like to shift?");
    String input = br.readLine();

    if(input.equals("quit")) break;
    else{
        int pos = Integer.parseInt(input);
        int temp = data[pos];

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

Of course you will need to do error checking on your input to make sure it is a valid position in the array. I am, however, posting this from a droid and coding on a phone is a pain.

This code also does a bit more than you are asking, but it gives an idea behind the logic of the while loop. Also, I just read your edit and I'm not sure I understand what exactly is going on, but again hope this may help. Would have put it as a comment but obviously it's a bit lengthy for that.

Upvotes: 1

Related Questions