Answers for "how to swap two elements in an arraylist java without collections"

2

swap items in arraylist java

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f"));
         
        System.out.println(list);
         
        Collections.swap(list, 1, 2);
         
        System.out.println(list);
    }
}
Posted by: Guest on November-27-2020
0

java list swap 2 elements

String a = words.get(0);
words.set(0, words.get(words.size() - 1));
words.set(words.size() - 1, a)
Posted by: Guest on June-03-2021

Code answers related to "how to swap two elements in an arraylist java without collections"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language