Write a Java program to swap two elements in an array list


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. public class Javacollectionexcercise {
  4. public static void main(String[] args) {
  5. ArrayList<String> Stringslist1= new ArrayList<String>();
  6. Stringslist1.add("Audi");
  7. Stringslist1.add("BMW");
  8. Stringslist1.add("Bugatti");
  9. Stringslist1.add("Ford");
  10. Stringslist1.add("Honda");
  11.  
  12. System.out.println("Array list before Swap:");
  13. for(String a: Stringslist1){
  14. System.out.println(a);
  15. }
  16.  
  17. Collections.swap(Stringslist1, 0, 2);
  18. System.out.println("Array list after swap:");
  19. for(String b: Stringslist1){
  20. System.out.println(b);
  21. }
  22. }
  23. }

Result

java techstudy.org

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.