java.util.Collections.swap() method is a java.util.Collections class method. It swaps elements at the specified positions in given list.
// Swaps elements at positions "i" and "j" in myList. public static void swap(List mylist, int i, int j) It throws IndexOutOfBoundsException if either i or j is out of range.
Original List : Original List : [code, practice, quiz, geeksforgeeks] After swap(mylist, 1, 2) : [code, quiz, practice, geeksforgeeks] After swap(mylist, 3, 1) : [code, geeksforgeeks, practice, quiz]
This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- Randomly select items from a List in Java
- Swapping Characters of a String in Java
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- List to Set in Java
- Set to List in Java
- Min and Max in a List in Java
- Convert List to Set in Java
- List to array in Java
- Immutable List in Java
- Initializing a List in Java
- How to Clone a List in Java?
- List of all Java Keywords
- Split a list into two halves in Java
- List get() method in Java with Examples