我得到了一个java.lang.ArrayIndexOutOfBoundsException,我认为这是因为当nums.length给我一个零(因为参数中没有插入任何内容)时我赋予变量的属性(-----。length -1 ------)当它给我零时会使它为-1。
我应该怎么做才不知道如果我把它留下.length而没有-1,那么数组的另一边会有一个超出范围的异常?
public boolean sameFirstLast(int[] nums) {
int lengthOfArray = nums.length - 1;
int thisIsHowLong = nums.length;
if (nums[0] == nums[lengthOfArray ] && thisIsHowLong > 0){
return true;
}
return false;
}
//always remember to return false in the end becuse if the top dosnt run then
//there will be no boolean returned and the method is not void so it has to return a //boolean
//ALWAYS REMEMBER TO SET nums.length - 1
//the .length method always starts from 1 and threfore will give you an extra space //that it will check for but we wont have anything in that space and then the compiler //will go to that space and will fond nothing to test with and gove an ERROR out of //bounds exeption because it is looking out of the array.