checking array boundary.

suggested by Gregory Collins.