Length
The number of elements which comprise an array can be accessed by using .length
.1
void main() {
String[] veggies = { "brussels", "cabbage", "carrots" };
int numberOfElements = veggies.length;
// veggies is 3 elements long
System.out.println(
"veggies is " + numberOfElements + " characters long"
);
}
1
Unlike with a String
, you do not write ()
after .length
.