I have to rotate numbers an array.

Example:{5, 6, 7, 8} ==> {6, 7, 8, 5}{5, 6, 7, 8, 9, 10} ==> {6, 7, 8, 9, 10, 5}

This is what I have. It works for the most part to rotate the numbersbut I can't seem to rotate the first number to the left(the last location);It says the the variable I saved it in (a) may not be declared. Also Im not sure how I can make it loop so if I have more that 9 numbers in my array it will still work.

public void rotateLeft(int[] theArray){]int a;
for(int i = 0; i < theArray.length ; i++)theArray[0] = a
theArray[0] = theArray[1];
theArray[1] = theArray[2];
theArray[2] = theArray[3];
theArray[3] = theArray[4];
theArray[4] = theArray[5];
theArray[5] = theArray[6];
theArray[6] = theArray[7];
theArray[7] = theArray[8];
theArray[8] = theArray[9];
theArray[9] = a;

OK Initialized a to Zero but It is not taking the vaule of array[0]

You're really close again. You've got the assignment backwards for the first value (it should be a = theArray[0]; ). There is one other problem with your code though: it doesn't scale. What you posted will rotate the first 10 items, and the loop will cause the rotation to happen however many times there are items in the array. So if the array had 12 items, the first 10 would get rotated 12 times. And if the array had less than 10 items, you'd get an exception. Here's another way to do it:
- save the first value in a temporary variable
- use a loop to iterate over the list, making theArray = theArray[i+1] (for values of i from 0 to theArray.length-1).
- copy the temporary value into the last item in theArray.

You'll notice that you need to adjust the condition in the for loop this time to have a -1. That's because you're indexing the (i+1)th element, so without the adjustment you'd get an ArrayIndexOutOfBoundsException on the last iteration.

Thanks here is my result;

int a = theArray[0];
int i;
for(i = 0; i < theArray.length-1; i++)
theArray[i] = theArray[i+1];
theArray[i]= a;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.