java shift elements in array

Posted by Lightk3ira on Stack Overflow See other posts from Stack Overflow or by Lightk3ira
Published on 2014-06-11T03:22:14Z Indexed on 2014/06/11 3:24 UTC
Read the original article Hit count: 217

Filed under:
|
|
|

Hey I am trying to shift elements forward sending the last element in the array to data[0]. I did the opposite direction but I can't seem to find my mistake in going in this direction.

Pos is users inputed shift times amount temp is the temporary holder. data is the array

if(pos > 0) { do { temp = data[data.length -1];

         for(int i =0; i < data.length; i++)
         {
            if(i == data.length-1)
            {
               data[0] = temp; 
            }
            else
            {
                data[i+1] = data[i];
            }
         }

         pos--;
      }while(pos > 0);
   }

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays