Arraylist swap elements

JavaArraylist

Java Problem Overview


How do I swap the the first and last elements of an ArrayList? I know how to swap the elements of an array: setting a temporary value to store the first element, letting the first element equal the last element, then letting the last element equal the stored first element.

int a = values[0];
int n = values.length;
values[0] = values[n-1];
values[n-1] = a;

So for an ArrayList<String> would it be like this?

String a = words.get(0);
int n = words.size();
words.get(0) = words.get(n-1);
words.get(n-1) = a

Java Solutions


Solution 1 - Java

You can use Collections.swap(List<?> list, int i, int j);

Solution 2 - Java

In Java, you cannot set a value in ArrayList by assigning to it, there's a set() method to call:

String a = words.get(0);
words.set(0, words.get(words.size() - 1));
words.set(words.size() - 1, a)

Solution 3 - Java

Use like this. Here is the online compilation of the code. Take a look http://ideone.com/MJJwtc

public static void swap(List list,
                        int i,
                        int j)

Swaps the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)

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.

Read The official Docs of collection

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#swap%28java.util.List,%20int,%20int%29

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);    

    }
}

Oneline compilation example http://ideone.com/MJJwtc

Solution 4 - Java

for (int i = 0; i < list.size(); i++) {
		if (i < list.size() - 1) {
			if (list.get(i) > list.get(i + 1)) {
				int j = list.get(i);
				list.remove(i);
				list.add(i, list.get(i));
				list.remove(i + 1);
				list.add(j);
				i = -1;
			}
		}
	}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionqvdView Question on Stackoverflow
Solution 1 - JavaEvgeniy DorofeevView Answer on Stackoverflow
Solution 2 - JavaneevekView Answer on Stackoverflow
Solution 3 - JavaNikhil AgrawalView Answer on Stackoverflow
Solution 4 - JavaPankajView Answer on Stackoverflow