6:01 am - Thursday August 17, 2017

Java Program used to ensure the minimum capacity of arraylist using ensureCapacity() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 EnsureCapacity program to ensure the minimum capacity of arraylist

Example Program


import java.util.ArrayList;

public class EnsureCapacity {

	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		al.add("10");
		al.add("30");
		al.add("60");
		al.add("70");
		al.add("80");
		al.add("90");
		al.add("10");
		al.add("20");
		al.add("100");
		al.ensureCapacity(8);
		for (String number : al) {
			System.out.println("numbers are" + number);
		}

	}

}


Output

numbers are 10

numbers are 30

numbers are 60

numbers are 70

numbers are 80

numbers are 90

numbers are 10

numbers are 20

numbers are 100

Description

The java.util.ArrayList.ensureCapacity(int minCapacity) method increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Declaration

Following is the declaration for java.util.ArrayList.ensureCapacity() method

public void ensureCapacity(int minCapacity)

Parameters

minCapacity — This is the desired minimum capacity

Return Value

This method does not return any value.

Exception

NA

BACK

Filed in: Z-A All Java Codes

Comments are closed.