Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use like this. Here is the online compilation of the code. Take a look <a href="http://ideone.com/MJJwtc" rel="noreferrer">http://ideone.com/MJJwtc</a> </p> <pre><code>public static void swap(List list, int i, int j) </code></pre> <p>Swaps the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)</p> <p>Parameters: list - The list in which to swap elements. i - the index of one element to be swapped. j - the index of the other element to be swapped. </p> <p>Read The official Docs of collection</p> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#swap%28java.util.List,%20int,%20int%29" rel="noreferrer">http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#swap%28java.util.List,%20int,%20int%29</a></p> <pre><code>import java.util.*; import java.lang.*; class Main { public static void main(String[] args) throws java.lang.Exception { //create an ArrayList object ArrayList words = new ArrayList(); //Add elements to Arraylist words.add("A"); words.add("B"); words.add("C"); words.add("D"); words.add("E"); System.out.println("Before swaping, ArrayList contains : " + words); /* To swap elements of Java ArrayList use, static void swap(List list, int firstElement, int secondElement) method of Collections class. Where firstElement is the index of first element to be swapped and secondElement is the index of the second element to be swapped. If the specified positions are equal, list remains unchanged. Please note that, this method can throw IndexOutOfBoundsException if any of the index values is not in range. */ Collections.swap(words, 0, words.size() - 1); System.out.println("After swaping, ArrayList contains : " + words); } } </code></pre> <p><strong>Oneline compilation example <a href="http://ideone.com/MJJwtc" rel="noreferrer">http://ideone.com/MJJwtc</a></strong> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload