Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, January 18, 2016

Shifting Elements in an Array

Shifting Elements in an Array


I have an array of objects, and I am trying to pull one element to the top and shift the rest down by one.

Assume I have an array of size 10, and I am trying to pull the fifth element. The fifth element goes into position 0 and all elements from 0 to 5 will be shifted down by one.

My algorithm is not working. Logically, it should work. Can someone please take a look at it? Assume position is the position I want to pull to the top of the array.

Object temp = pool[position];    for (int i = 0; i < position; i++) {                      array[i+1] = array[i];  }    array[0] = temp;  

Thank you

Answer by thejh for Shifting Elements in an Array


Try this:

Object temp = pool[position];    for (int i = position-1; i >= 0; i--) {                      array[i+1] = array[i];  }    array[0] = temp;  

Look here to see it working: http://www.ideone.com/5JfAg

Answer by Howard for Shifting Elements in an Array


Logically it does not work and you should reverse your loop:

for (int i = position-1; i >= 0; i--) {                      array[i+1] = array[i];  }  

Alternatively you can use

System.arraycopy(array, 0, array, 1, position);  

Answer by Paul Sasik for Shifting Elements in an Array


Manipulating arrays in this way is error prone, as you've discovered. A better option may be to use a LinkedList in your situation. With a linked list, and all Java collections, array management is handled internally so you don't have to worry about moving elements around. With a LinkedList you just call remove and then addLast and the you're done.

Answer by Komal Padia for Shifting Elements in an Array


Assuming your array is {10,20,30,40,50,60,70,80,90,100}

What your loop does is:

Iteration 1: array[1] = array[0]; {10,10,30,40,50,60,70,80,90,100}

Iteration 2: array[2] = array[1]; {10,10,10,40,50,60,70,80,90,100}

What you should be doing is

Object temp = pool[position];    for (int i = (position - 1); i >= 0; i--) {                      array[i+1] = array[i];  }    array[0] = temp;  

Answer by BenH for Shifting Elements in an Array


In the first iteration of your loop, you overwrite the value in array[1]. You should go through the indicies in the reverse order.

Answer by expert for Shifting Elements in an Array


static void pushZerosToEnd(int arr[])      {   int n = arr.length;          int count = 0;  // Count of non-zero elements          // Traverse the array. If element encountered is non-zero, then          // replace the element at index 'count' with this element          for (int i = 0; i < n; i++){              if (arr[i] != 0)`enter code here`                 // arr[count++] = arr[i]; // here count is incremented                  swapNumbers(arr,count++,i);          }          for (int j = 0; j < n; j++){              System.out.print(arr[j]+",");          }       }        public static void swapNumbers(int [] arr, int pos1, int pos2){          int temp  = arr[pos2];          arr[pos2] = arr[pos1];          arr[pos1] = temp;      }  

Answer by Murat Mustafin for Shifting Elements in an Array


You can just use Collections.rotate(List list, int distance)

Use Arrays.asList(array) to convert to List

more info at: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#rotate(java.util.List,%20int)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.