Java - Rotating array -



Java - Rotating array -

so goal rotate elements in array right a times. example; if a==2, array = {0,1,2,3,4} become array = {3,4,0,1,2}

here's have:

for (int x = 0; x <= array.length-1; x++){ array[x+a] = array[x]; }

however, fails business relationship when [x+a] greater length of array. read should store ones greater in different array seeing a variable i'm not sure that's best solution. in advance.

add modulo array length code:

// create newarray before of same size array // re-create for(int x = 0; x <= array.length-1; x++){ newarray[(x+a) % array.length ] = array[x]; }

you should create new array re-create to, not overwrite values, you'll need later on.

java arrays rotation

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -