Knowledge Walls
John Peter
Pune, Maharashtra, India
Swap method of java collections with example
2196 Views
SwapMethodExample
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SwapMethodExample {
    public static void main(String args[]){
        List<String> dataList = new ArrayList<String>();
            dataList.add("Ball");
            dataList.add("Apple");
            dataList.add("Dog");
            dataList.add("Cat");
        
        System.out.println("Before");
        System.out.println(dataList);
        
        // 2 is a index of Dog
        // 3 is a index of Cat
        // This below statement will swap Dog and Cat
        Collections.swap(dataList, 2, 3);
        
        System.out.println("After");
        System.out.println(dataList);
    }
}
Output 
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details