Wednesday, 1 July 2015

Java : Collection Framework : Collections (Swap)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=hmNmo-iUik0&list=UUhwKlOVR041tngjerWxVccw

CollectionsExample.java
import java.util.ArrayList;
import java.util.Collections;

/*
 Example of:  

 swap(List<?> list, int i, int j)

 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.

 */

public class CollectionsExample
{

    public static void main(String[] args)
    {

        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        arrayList.add(2000);
        arrayList.add(3000);
        arrayList.add(4000);
        arrayList.add(1000);

        System.out.println("arrayList : " + arrayList + "\n");

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

        Collections.swap(arrayList, 0, 1);

        System.out.println("After Swap , arrayList : " + arrayList + "\n");

    }

}
Output
arrayList : [2000, 3000, 4000, 1000]

After Swap , arrayList : [3000, 2000, 4000, 1000]
To Download CollectionsDemoSwapApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoSwapApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment