想取出两个元素进行比较,但是提示超出索引范围
为什么呢?
你把 ArrayList 的构造函数的参数去掉,这样:
ArrayList<Integer> eachNum = new ArrayList<Integer>();
ArrayList<Integer> eachNum2 = new ArrayList<Integer>();
然后就不会有异常了。为什么呢?
因为在 get(int) 函数里面,首先会执行 rangeCheck(int) 方法,而 rangeCheck(int) 方法是这样的:
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
在你的代码里面,index 与 size 相等了,所以会有异常。如果在构造函数中不给 size 传参,那么每次在 add 的时候会自动判断 size 够不够用,如果不够则会新开辟一段更大的空间然后内容复制过去。
所以,你只需要使用无参构造函数就可以了。
如果有帮助请采纳,谢谢!(我需要积分~)
q是secret的长度,guess不一定有那么长
根据异常提示 问题应该是处在第二个for循环 单指按照给出的代码块 机上测试是没问题的