programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Rotate left (arrays)

Ranch Hand
Posts: 62
2
Here is the challenge:

Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.

rotateLeft3([1, 2, 3]) → [2, 3, 1]
rotateLeft3([5, 11, 9]) → [11, 9, 5]
rotateLeft3([7, 0, 0]) → [0, 0, 7]

My code works, but I feel there could be an easier way to do this?  Or is this fine?  If it was a bigger array I would probably need to use a loop?

Saloon Keeper
Posts: 8307
148
Yes, a loop would be better, but if you're allowed to use collections, you can do this extremely easily by converting an array to a Queue, and then removing the first element and then adding it back to the end of the queue. At the end, you can convert the queue back to an array.

Two good Queue implementations you can use for this are ArrayDeque and LinkedList.

author
Sheriff
Posts: 23364
127

You can also very easily accomplish it with a temporary variable, and a call to the System.arraycopy() method.

Henry

Sheriff
Posts: 11629
187

Bod MacNeil wrote:
My code works, but I feel there could be an easier way to do this?  Or is this fine?  If it was a bigger array I would probably need to use a loop?

You could actually do this in place, with just one temporary int variable as the additional space requirement. The way you're doing it and the other ones suggested so far require 2N space for an array with N elements. Basically, you note down what the first element is, then shift all other elements down one spot in the array using a loop, then put the first element's value in the last spot.

Junilu Lacar
Sheriff
Posts: 11629
187
Of course, there are pros and cons to an in-place solution. The pro is that you minimize your memory requirements, needing only the temporary variable to remember what the original first element value was.  The potential con is that you are mutating the array.  This may or may not be a good thing depending on how that array is used by the rest of the program.  As an exercise, it doesn't matter much unless the exercise goal is to find a memory-efficient solution, a time-efficient solution, or a solution that will work in a multi-threaded context. If there's concurrency involved, it's generally better to work with data that is immutable.

Marshal
Posts: 57437
175
Also please tell us where that code comes from, to void copyright problems and so we can easily find it. It looks rather like something out of coding bat.

Bod MacNeil
Ranch Hand
Posts: 62
2
Thanks guys.  Yes its from CodingBat and I will link it next time as I'll probably be posting more stuff from there.  I tried to use  LInkedList but I'll need to practice with them more as I can't convert my array to it and then back.  I'll try something ese that was mentioned in the mean time.  cheers!

Campbell Ritchie
Marshal
Posts: 57437
175
Which exercise number was it? I looked on coding bat earlier and couldn't find that question.

Bartender
Posts: 10575
66

Stephan van Hulst wrote:Yes, a loop would be better, but if you're allowed to use collections, you can do this extremely easily by converting an array to a Queue, and then removing the first element and then adding it back to the end of the queue. At the end, you can convert the queue back to an array.

@Bod: Good advice. However, if you want to do it yourself and make it generic (which I suspect may be the object of the exercise) have a look at the % operator, and think about how it might help you.
(Hint: rotateLeft(n) is usually the same as rotateRight(length - n), providing 0 < n < length).

Winston