Write a Java program to increase the size of 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

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. public class Javacollectionexcercise {
  4. public static void main(String[] args) {
  5. ArrayList<String> Stringslist= new ArrayList<String>(3);
  6. Stringslist.add("Audi");
  7. Stringslist.add("BMW");
  8. Stringslist.add("Bugatti");
  9. System.out.println("Original array list: " + Stringslist);
  10. //Increase capacity to 7
  11. Stringslist.ensureCapacity(7);
  12. Stringslist.add("Ford");
  13. Stringslist.add("Honda");
  14. Stringslist.add("Hyundai");
  15. Stringslist.add("Ferrari");
  16. System.out.println("New Increased array list: " + Stringslist);
  17. }
  18. }

Result

java techstudy.org

     





Related Topics




Popular Posts