Length of an object array
You can use getLength
method from java.lang.reflect.Array
, which uses reflection (so it'll be slow):
Object[] anArray = new Object[2];
int length = Array.getLength(anArray);
No matter the type an array is, we only have .length
but NOT .length()
Arrays in Java are not resizable. Once an array is instantiated, its length cannot change. That's why the length
attribute (myArray.length
) can always be trusted to contain the array's length.