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
- How to add all items from a collection to an ArrayList in Java?
- How to add selected items from a collection to an ArrayList in Java?
- Program to convert List of String to List of Integer in Java
- Program to convert List of Integer to List of String in Java
- How to iterate over a 2D list (list of lists) 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
- Initializing a List in Java
- List of all Java Keywords
- List to array in Java
- How to Clone a List in Java?