Close

Java Collections - Collections.swap() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Method:

public static void swap(List<?> list,
                        int i,
                        int j)

Swaps the elements at the specified positions in the specified list.


Examples


package com.logicbig.example.collections;

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

public class SwapExample {

public static void main(String... args) {
List<String> list = Arrays.asList("one", "two", "three", "four");
System.out.println(list);

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

}
}

Output

[one, two, three, four]
[one, four, three, two]




See Also