programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Knute Snortum
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Ganesh Patekar
• Frits Walraven
• Tim Moores
• Ron McLeod
• Carey Brown
Bartenders:
• Stephan van Hulst
• salvin francis
• Tim Holloway

# Rotating an array of integers

Greenhorn
Posts: 5
Hello,people.I am a beginner and I'm struggling here.How can i write a program to rotate an array of integers in the left direction? If someone could help me,i would frankly appreciate it.Thanks

Rancher
Posts: 3306
33
Can you post an example of an array with before and after rotation shown so we can see what you are talking about?

irini irene
Greenhorn
Posts: 5
original: 1 3 6 7 4 5 7 7 9 9
rotated : 3 6 7 4 5 7 7 9 9 1

Rancher
Posts: 3306
33
Ok, thanks for that.
Next how would you do it manually?  What if there were some playing cards laid in a row in slots on the table?  What steps would you take to rotate them?

irini irene
Greenhorn
Posts: 5
I would pick up the first card.Then this position would be empty.And then i would say that every card should be placed one position to  its left.So instead of the place 2,the second card would be at place 2-1 etc.

Rancher
Posts: 3306
33
• 1
Ok that sounds like it could work.

Try writing the code to do that. You could use a for loop to move the index through the array.

Sheriff
Posts: 23866
50
• 1
That sounds like a good start. So why don't you write the code for that and see what happens?

Rancher
Posts: 484
11
I had posted the solution here, but Norm is right, it was spoon feeding and it was never my intention to rob you of the pleasure of trying to solve it, so try solving it yourself and when you can't post the code you already have and we will give you some pointers.

Rancher
Posts: 3306
33

Marshal
Posts: 61690
192
Daniel Demesmaecker: thank you for deleting that code with good grace It takes a good person to acknowledge mistakes like that.

Please look at the link Norm posted; it is a good explanation of why posting complete solutions is a bad thing. I looked at it last night, but didn't finish it because it was very late and it is a long article.
But, I am going to retrieve your code (obviously moderators have access to deleted posts on every forum website) because I think it needs discussion. I am afraid I don't actually think it is correct code. You would have noticed that if you had tested it more extensively. I suggest you go through the original array and make ten copies of it, each with one number changed. For example, if the original array is
1 3 6 7 4 5 7 7 9 9
then your first change might be
2 3 6 7 4 5 7 7 9 9
and your fifth change might be
1 3 6 7 5 5 7 7 9 9
You might like to write a program to make those changes automatically.
You now have eleven different arrays: test your program with those eleven and see how many give the expected output. I am going to be bold and predict that you will get 9/11 right.
I am afraid you are going to have to improve your code style. I can't seem to find the old Sun style guide; I think it has been removed. If you find it pleasee post a link to it. We do however have old Javaranch style suggestions. Have a look at the sections about spacing out text. I also think the if statement would work better if you move it outside the loop.

Challenge: Find out whether I was right about 2/11. I haven't tried it yet. I was all confused last night about why your code seemed to give the expected answer, but when daylight come, I thought there is a special feature about the input which obscures the error. See if you can work out what I mean. And whether I am right or wrong.

Campbell Ritchie
Marshal
Posts: 61690
192
This is the code which Daniel Demesmaecker deleted:-

Daniel Demesmaecker
Rancher
Posts: 484
11
I don't know if this is what you mean by the odl sun style guide, but anyways: 1997 Java code conventions

Daniel Demesmaecker
Rancher
Posts: 484
11
You're right the code isn't correct...

Daniel Demesmaecker
Rancher
Posts: 484
11
Changing the numbeers don't present a problem, but iterating the whole code does. I looped it 15 times and didn't get the expected result.

Campbell Ritchie
Marshal
Posts: 61690
192

Daniel Demesmaecker wrote:. . . odl sun style guide, but anyways: 1997 Java code conventions

Yes, that is what I meant, but the link won't open.

Campbell Ritchie
Marshal
Posts: 61690
192
This is how I made the ten changes:-It has all the hallmarks of something written in three minutes, otherwise it wouldn't have the repetition of lines 4 and 8.

Campbell Ritchie
Marshal
Posts: 61690
192
• 1
Daniel Demesmaecker, please can I ask you to delay posting anything else on this thread until tomorrow. That will give irini irene a chance to solve the problem for herself. And thank you for the Sun link.

Daniel Demesmaecker
Rancher
Posts: 484
11

Campbell Ritchie wrote:

Daniel Demesmaecker wrote:. . . old sun style guide, but anyways: 1997 Java code conventions

Yes, that is what I meant, but the link won't open.

Weird, I have no probs opening the link

Campbell Ritchie
Marshal
Posts: 61690
192
• 1
I shall try the style guide link again, but I shall have to install a different browser first.

There has been nothing from OP for 36  hours, so I think I can discuss your solution with a clear conscience.

Some languages permit this construct: xy := yx, but the languages whose syntax derives from C don't. And that includes Java®. Now, if you try to swap the values of two variables, you have to use an intermediate temporary variable. Otherwise you get this sort of thing:-I know there are party tricks like this:-...but their behaviour in C is not strictly defined (I think, not certain), and the left‑to‑right rule in Java® means that trick doesn't work at all in Java®. Try it for yourself, both in C and Java,, but remember it isn't suitable for production code. There are also tricks whereby you subtract the two values, but I can't remember how they go.
That means that swapping two elements in an array is best done in a utility method, so please remind yourself about the utility class pattern (I think it is a pattern, even though I don't remember seeing it in the pattern books).
• 1: The class has no variables.
• 2: It may have constants.
• 3: It may have methods.
• 4: It may have both constants and methods.
• 5: Since its methods never change its state (there isn't any state to change anyway), they can all be marked static.
• 6: Since all its members are static, there is never any need to instantiate a utility class.
• A good examples of a utility class is java.lang.Math. If you search the contents of the Java® Language Specification (=JLS) with ctrl‑F‑preventing instantiation, you will find a little tutorial about that. Note that technique also makes it impossible to subclass your utility class. You end up with a utility method like this:-Also have a look at my IntSwapper class. I am sure you won't need any more explanation about how it works

Now we have seen that, consider that rotating an array takes the same concepts as swapping elements, only it is a bigger exercise. You have successfully avoided simply copying array[0] into every element of the array, but go through the code you posted very carefully and work out how many times you have assigned anything to array[8]. In 9 out of 11 cases, you were unlucky that the last two elements of the array were equal to each other, so leaving the penultimate element unchanged appeared to give the required result. The error only shows up when the last two elements are different from each other. There are two corrections I would suggest.
• 1: Retain a temporary variable equal to array[0]
• 2: Iterate the whole length of the array.
• I am now leaving you with a challenge to write two rotate methods. One will rotate an array one place to the left, and the other will be a universal rotate method which accepts any int as a parameter rotating the array either left or right depending on its sign.
• Reminder: % operator.
• Reminder: the only thing you can do with a two‑element array is swapTwoElements(array, 0, 1);
• Reminder: You can rotate an array with < 2 elements as much as you like, but its state will always revert to its original state.
•
Campbell Ritchie
Marshal
Posts: 61690
192

A few minutes ago, I wrote:. . . .

• 1: Retain a temporary variable equal to array[0]
• 2: Iterate the whole length of the array.
• . . . .

There are of course other ways to rotate an array. You can probably rotate it going backwards.

Paul Clapham
Sheriff
Posts: 23866
50
I know this is an exercise to train beginners in the use of arrays, but...

You can simplify the process greatly by using the System.arrayCopy() method. Like so:

1. Save array[0] somewhere.

2. Use System.arrayCopy() to copy array[1..N] to array[0..N-1].

3. Put the saved value back into array[N].

Campbell Ritchie
Marshal
Posts: 61690
192
Or even if there were an Arrays.rotate() method. But there isn't.

Bartender
Posts: 1970
57

Campbell Ritchie wrote:I know there are party tricks like this:-

Campbell Ritchie
Marshal
Posts: 61690
192
That is obviously the subtraction trick I couldn't remember on Sunday.

Marshal
Posts: 6251
420

Paul Clapham wrote:You can simplify the process greatly by using the System.arrayCopy() method.

Wouldn't be even simpler to convert to list and use Collections.rotate() and then back to an array?

Campbell Ritchie
Marshal
Posts: 61690
192

Daniel Demesmaecker wrote:. . . anyways: 1997 Java code conventions

That is the document, yes. Thank you. I can open it on Opera and not on FF, but I am too busy for that to be more than one of those little mysteries of life.

[edit:addition] It is a different edition from what I was familiar with, but yes, that is the document. I have saved it.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?