| | |
Shifting Elements in an Array k places to the right.
![]() |
•
•
Join Date: Sep 2004
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
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
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
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.
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.
![]() |
Similar Threads
- Finding No. of elements in an Array (C)
- Reversing the elements of an array (Java)
- Converting specified elements of array of char (C++)
- A neater way to print elements in an array? (Java)
Other Threads in the Java Forum
- Previous Thread: Question about object lock
- Next Thread: Threads
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows






