Write a Java program to increase the size of an array list


The program starts by creating an ArrayList of String type named list_Book with an initial capacity of 3 using the constructor of ArrayList. The program then adds three elements to the list using the add() method of ArrayList. The elements added to the list are "C++", "Java", and "HTML". Then, the program prints the elements of the list using the println() method of System.out object.

After that, the program increases the capacity of the list to 7 using the ensureCapacity() method of the ArrayList class. Then, the program adds four more elements to the list using the add() method of ArrayList. The elements added to the list are "C", "Php", "Css", and "MySQL". The program then prints the elements of the list using the println() method of System.out object.

This is because the ensureCapacity() method of the ArrayList class is used to increase the capacity of the list to 7. The add() method of ArrayList is used to add four more elements to the list. The elements added to the list are "C", "Php", "Css", and "MySQL". The program then prints the elements of the list using the println() method of System.out object.

Source Code

import java.util.ArrayList;
import java.util.Collections;
public class Increase_Size
{
	public static void main(String[] args)
	{
		ArrayList<String> list_Book= new ArrayList<String>(3);
		list_Book.add("C++");
		list_Book.add("Java");
		list_Book.add("HTML");
		System.out.println("Original Array List : " + list_Book);
		list_Book.ensureCapacity(7);
		list_Book.add("C");    
		list_Book.add("Php");
		list_Book.add("Css");
		list_Book.add("MySQL");
		System.out.println("New Increased Array List : " + list_Book);
	}
}

Output

Original Array List : [C++, Java, HTML]
New Increased Array List : [C++, Java, HTML, C, Php, Css, MySQL]

Example Programs