How do I get the length of an array?

John Zukowski

To avoid getting an ArrayIndexOutOfBoundsException, you can check for the array length from either the length instance variable or using reflection and calling java.lang.reflect.Array.getLength(), passing the array as an argument to the method.

int length = args.length;
// or
int length2 = Array.getLength(args);