944,065 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 15361
  • Java RSS
Feb 4th, 2005
0

Shifting Elements in an Array k places to the right.

Expand Post »
Hi I need to write a method that will rotate the elemnts in an array by a factor of k. ie shift each element k places to the right.

Here is the question i have been set

In the class ArrayLinearList a linear list is represented in an one-dimensional array element. The data member size is such that the list elements are in positions 0 through size-1 of the array. Write a new method rotate that will rotate the elements of the linear list k positions to the right. For example, if the list element[0:5] = [1, 2, 3, 4, 5, 6] is rotated by k=3, the result is [4, 5, 6, 1, 2, 3].

I've tried by inserting an element once i've shifted the elements one place to the right but I'm sure that this is incorrect. If anyone can be of any help I will be very grateful.

Mus.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mus_203 is offline Offline
11 posts
since Sep 2004
Feb 4th, 2005
0

Re: Shifting Elements in an Array k places to the right.

Hi everyone,

What you can do is get the factor and add the array position of the variable to that factor and then that will be the new array position of the variable

If the new variable position in the array exceeds the array length ,minus the the array length from the new array position of the variable.

Put this in a loop and loop it for all the variables in the array

I hope this helps you

Yours Sincerely

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Feb 4th, 2005
0

Re: Shifting Elements in an Array k places to the right.

Remember that arrays in Java don't grow so you need to take care of the buffering yourself.

If you move say 5 places to the right, create a buffer of 5 elements.
Move the last 5 elements out of the array into the buffer, then move every element in the array (except those 5) 5 places to the right.
Finally most those 5 elements into the first 5 positions in the original array.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 6th, 2005
0

Re: Shifting Elements in an Array k places to the right.

Thanks a lot for that. Really appreciate it.

Mus.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mus_203 is offline Offline
11 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Question about object lock
Next Thread in Java Forum Timeline: Threads





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC