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.
- Swapping Characters of a String 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
- Set to List in Java
- List to Set in Java
- Min and Max in a List in Java
- Initializing a List in Java
- List of all Java Keywords
- List to array in Java
- Immutable List in Java
- Convert List to Set in Java
- Conversion of Java Maps to List
- Shuffle or Randomize a list in Java
- List Interface in Java with Examples
- Program to Convert List to Map in Java