-4

How to move element of array to specific position on Android JAVA. We have

int oldPosition, int newPosition

and some like

JSONObject[] tmp = new JSONObject[999];

array of JSONObjects

Alex K
  • 22,315
  • 19
  • 108
  • 236
  • Use temp variable to store the current value and move the old position value to the temp variable then, move the new position value to the old position finally move the temp value into new position. – Manoj Prabhakar M Dec 29 '16 at 07:35
  • Welcome, please provide the code you have tried to show us that you have take some times on this problem. And of course, you should search for similar question first, this is a simple algorithm problem. – AxelH Dec 29 '16 at 11:18

3 Answers3

0

If you want to just move tmp[newPosition]=tmp[oldPosition];

Swap

JSONObject jo= tmp[oldPosition]; tmp[oldPosition]=tmp[newPosition]; tmp[newPosition]=jo;

Mihodi Lushan
  • 670
  • 5
  • 24
0

EDIT: There are other ways but you can go through this as well to get your result :)

Exercise : You can understand this logic and use JSONObject type and do necessary changes, Watch out for NullPointerExceptions , handle everything i am lazy to do them

let's say you have int array --> private int array[];

 array = new int[]{10,20,30,40,50,60,70,80,90,100};

call this method if you want to swap elements,

swapNumbers(array,9,1);

.

 public  int[]  swapNumbers(int [] arr, int possition1, int possition2){
    int temp  = arr[possition2];
    arr[possition2] = arr[possition1];
    arr[possition1] = temp;
    System.out.println("array -->" + Arrays.toString(array));
    return arr;
}

out put : array[10, 100, 30, 40, 50, 60, 70, 80, 90, 20]


But that doesn't satisfy you?

you need out put to be like this : array[10, 100, 20, 30, 40, 50, 60, 70, 80, 90]

you can use below method

resetUpMyArray(array, array[9],1);
System.out.println("array Finally changed-->" + Arrays.toString(array));

enjoy,

public int[] resetUpMyArray(int[] inputArray, int deleteMeValue,int addMePosition) {
            List resultLinkedList = new LinkedList();

            for (int itemValue : inputArray)
                if (deleteMeValue == itemValue) {
                    System.out.println("Do not add this value"+itemValue);
                } else {
                    System.out.println("Do add this value "+itemValue +"position-"+deleteMeValue);
                    resultLinkedList.add(itemValue);
                }

            System.out.println("array -as new L.L->" + resultLinkedList);
            resultLinkedList.add(addMePosition,deleteMeValue);
            System.out.println("array -as new L.L all set->" + resultLinkedList);

            array = new int[resultLinkedList.size()];
            for (int i = 0; i < resultLinkedList.size(); i++) {
                array[i] = (int) resultLinkedList.get(i); // Watch out for NullPointerExceptions!
            }

            return array;
        }
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • 1
    You should have provide a pseudo code for this, this is more generic (and force OP to do somethind ;) ). But there is easier solution to shift an array from one index to another using the same kind of logic as swapNumbers with a loop too shift the values. – AxelH Dec 29 '16 at 11:22
  • @AxelH ;) Yes next time ill keep that in mind.This thing came to my mind and i posted it.Good point from you .Thank you! – Charuක Dec 29 '16 at 11:25
  • I have writen the algo for the simple solution. It is using the same array instead of a List. – AxelH Dec 29 '16 at 11:45
0

Here is two simple algorithm.

Switch values :

switch(array, from, to)
    tmp         = array[to]
    array[to]   = array[from]
    array[from] = tmp

That will give something like

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
          |--<---->--|
[10, 20, 60, 40, 50, 30, 70, 80, 90, 100]

This will simply store the values that will be replace at the index to to be place at index from

Move and shift values:

This one will move one values a shift the values next.

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
          |------------^
[10, 20,   , 40, 50, 60, 30, 70, 80, 90, 100]
           <-----------
[10, 20, 40, 50, 60, 30, 70, 80, 90, 100]

For this, the solution is quite the same, store the values in tmp but shift every value to fill the gap. This code will only work if from < to

tmp = array[to]    
i = from
while(i < to)
    array[i] = array[i+1]; --Shift the value
    i = i + 1       
array[to] = tmp;
AxelH
  • 14,325
  • 2
  • 25
  • 55