
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do I set the size of a list in Java?
Java list size is dynamic. It increases automatically whenever you add an element to it and this operation exceeds the initial capacity. You can define the initial capacity at the time of list creation so that it allocates memory after initial capacity exhausts.
List<Integer> list = new ArrayList<Integer>(10);
But please don't use index > 0 to add element, otherwise you will get IndexOutOfBoundsException as the index will out of range considering size is 0 and index > size().
List provides size() method to get the count of the elements present in the list.
Syntax
int size()
Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements returns Integer.MAX_VALUE.
Example
Following is the example showing the usage of size() method −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3)); System.out.println("List: " + list); System.out.println("List size: " + list.size()); list.add(4); list.add(5); list.add(6); System.out.println("List: " + list); System.out.println("List size: " + list.size()); list.remove(1); System.out.println("List: " + list); System.out.println("List size: " + list.size()); } }
Output
This will produce the following result −
List: [1, 2, 3] List size: 3 List: [1, 2, 3, 4, 5, 6] List size: 6 List: [1, 3, 4, 5, 6] List size: 5
- Related Articles
- How do I find the size of a Java list?
- How do I set a minimum window size in Tkinter?
- How do I empty a list in Java?
- How do I search a list in Java?
- How do I insert elements in a Java list?
- How do you create a list from a set in Java?
- How do you turn a list into a Set in Java?
- How do we get the size of a list in Python?
- How do we get size of a list in Python?
- Set the size of a Vector in Java
- How do I get length of list of lists in Java?
- How do I remove multiple elements from a list in Java?
- How do I turn a list into an array in Java?
- How do I find an element in Java List?
- How do I insert elements at a specific index in Java list?

Advertisements