1

I have an ArrayList that contains the following elements:
"A", "B", "C", "D"
I want to move "A" after "C" to have the following order:
"B", "C", "A", "D"
I use this code:

 int sourceIndex = arrayList.indexOf("A");
 int destIndex = arrayList.indexOf("C");
 arrayList.add(destIndex, "A");
 arrayList.remove(sourceIndex);

the result of the above code looks like this:
"B", "A", "C", "D"
What is the best way to move one element after another element?

user6931342
  • 145
  • 3
  • 11

3 Answers3

4

Swap the order of adding and deleting the elements to make it work. Note that whenever you remove an element from an ArrayList, all the indices after that position get decreased by one place; in other words, all the elements after that position get shifted to the left (or towards the beginning) by one position.

Demo:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));

        int sourceIndex = list.indexOf("A");
        int destIndex = list.indexOf("C");
        
        list.remove(sourceIndex);
        list.add(destIndex, "A");
        
        System.out.println(list);
    }
}

Output:

[B, C, A, D]

There can be many other ways to do it e.g.

Rotate and swap:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("A", "B", "C", "D");

        Collections.rotate(list, 3);
        System.out.println(list);

        Collections.swap(list, 2, 3);
        System.out.println(list);
    }
}

Output:

[B, C, D, A]
[B, C, A, D]

Just swap:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("A", "B", "C", "D");
        
        Collections.swap(list, 0, 1);
        System.out.println(list);
        
        Collections.swap(list, 1, 2);
        System.out.println(list);
    }
}

Output:

[B, A, C, D]
[B, C, A, D]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

In this particular case you can modify your initial "A", "B", "C", "D" list to the "B", "C", "A", "D" list with arrayList.add(destIndex + 1, "A") instead of arrayList.add(destIndex, "A") like below:

int sourceIndex = arrayList.indexOf("A");
int destIndex = arrayList.indexOf("C");
arrayList.add(destIndex + 1, "A");
arrayList.remove(sourceIndex);

Be aware that List:add and List:remove will throw an IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()), so this code cannot be applied to every scenario.

dariosicily
  • 4,239
  • 2
  • 11
  • 17
1

Thank you all, I finally used this code:

 public static void moveElementAfter(ArrayList<String> arrayList,String src, String dst) {
    int sourceIndex = arrayList.indexOf(src);
    int destIndex = arrayList.indexOf(dst);
    // copy source element after destination
    arrayList.add(destIndex + 1, src);

    if (sourceIndex < destIndex) {
        // * the source element is moved forward in the array list
        // remove the source element
        arrayList.remove(sourceIndex);
    } else {
        // * the source element is moved backward in the array list
        // remove the source element
        int sourceLastIndex = arrayList.lastIndexOf(src);
        arrayList.remove(sourceLastIndex);
    }// end if
    System.out.println(arrayList);
}// end method

Demo

System.out.println("move A after C");
ArrayList<String> arrayList1  = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
moveElementAfter(arrayList1, "A", "C");

System.out.println("move C after A");
ArrayList<String> arrayList2  = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
moveElementAfter(arrayList2, "C", "A");

Output

[B, C, A, D]
[A, C, B, D]
user6931342
  • 145
  • 3
  • 11