Java Collections swap() MethodThe swap() method of Java Collections class is used to swap the elements at the specified positions in the specified list. SyntaxFollowing is the declaration of swap() method: Parameter
ReturnsThe swap() method does not return anything. ExceptionsIndexOutOfBoundsException- It will thrown this exception if the i or j is out of range. Compatibility VersionJava 1.4 and above Example 1Test it NowOutput: Original List : [Java, Android, Python, Node.js] List after swapping : [Python, Android, Java, Node.js] Example 2Test it NowOutput: List before swapping: [44, 55, 99, 77, 88, 66] List after swapping: [44, 55, 66, 77, 88, 99] Example 3Test it NowOutput: List before swapping: [44, 55, 99, 77, 88, 66, 33, 22] Enter index i : 2 Enter index j : 7 List after swapping: [44, 55, 22, 77, 88, 66, 33, 99] Next TopicJava Collections Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India