[java] How to get first and last element in an array in java?