Java ArrayList size() Method · Private Archive
Java ArrayList size() Method
The size() method is used to return the number of elements in the dynamic array.
arraylist.size()
**Note:** arraylist is an object of the ArrayList class.
Parameter Description:
- None
Return Value
The syntax of the size() method is:
Example
Returns the number of elements in the array.
Example
import
java.util.ArrayList
;
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
// Create a dynamic array
ArrayList
<
String
sites
=
new
ArrayList
<>
(
)
;
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
System
.
out
.
println
(
"Website list: "
sites
)
;
Getting the length of the dynamic array:
int
size
=
sites.
size
(
)
;
System
.
out
.
println
(
// Get the number of elements in the array
size
)
;
}
}
The output of the above program is:
网站列表: [Google, Runoob, Taobao]
动态数组长度: 3
"Dynamic array length: "
Java ArrayList