swaps two entries of an ArrayList - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

swaps two entries of an ArrayList

Demo Code


//package com.java2s;
import java.util.ArrayList;

public class Main {
    /**// w w w. j  a v a  2 s. c om
     * swaps two entries of an ArrayList
     * 
     * @param arrayList  the ArrayList whose entries you want to swap
     * @param yin  the index of the first swapped entry
     * @param yang  the index of the other swapped entry
     */
    public static <T extends Comparable<? super T>> void swap(
            ArrayList<T> arrayList, int yin, int yang) {
        T qi = arrayList.get(yin);
        arrayList.set(yin, arrayList.get(yang));
        arrayList.set(yang, qi);
    }
}

Related Tutorials