在 APlusCompSci Odd to Even 数组问题中不断获取 IndexOutOfBoundsException
Keep getting IndexOutOfBoundsException in APlusCompSci Odd to Even array problem
问题: 编写一个方法来检查数组中是否有奇数和偶数。找到第一个奇数。然后,在找到奇数之后,开始寻找偶数的位置,然后 return 它们之间的距离。 Return -1 如果没有奇数或没有奇数后跟偶数。
Data:包含 0 个或多个值的数组。该数组不会指向 null。
输出:Return距离或-1。
示例数据:
7、5、10、12
3、5、7、1
2、4、3、8、10、12
11
5、3、11、6、7、11、8
示例输出
2
-1
1
-1
3
这里是对apluscompsci.com的一个问题的介绍。这个问题是在我的 AP Computer Science class 中分配给学生的一个问题。所以,我对这个问题的代码是这样的:
int go( int[] array )
{
int o = 1;
int e = 0;
int distance = e-o;
if(array.length==0)
{
return distance;
}
for(int a = 0, y = 0; a < array.length || y<1; a++)
{
if(array[a]%2==1)
{
y++;
o = a;
for(int b = a + 1, x = 0; b < array.length || x<1; b++)
{
if(array[b]%2==0)
{
x++;
e = b;
}
}
}
}
if(e==0)
{
o=1;
}
return distance;
}
出于某种原因,我为解决问题而构建的代码不断触发 "java.lang.ArrayIndexOutOfBoundsException" 错误。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at Submission.go(typed.java:30)
at Submission.main(typed.java:9)
不过,这是不可能的,因为我已经以最抽象的方式编写了这个解决方案,而且我的 for 循环从不请求不存在的索引.即使是老师或房间里最聪明的孩子也无法帮助我找出答案,我已经调整了这段代码将近 3 天。我已经尝试通过在 Eclipse 中重建代码来进行故障排除,以防本网站的 JavaScript 可能不支持我使用的一些额外技术,例如我的 for 循环中的多变量启动和 or 运算符,但它吐出同样的错误。
另外,这是我在这个网站上的第一个问题!我其实只是注册了一个帐户来问这个,哈哈!希望我把所有内容都简单明了 - 在此先感谢您的帮助!
这是一个伪代码解决方案
for each number in array
if is looking for odd and number is odd
remember position
set is looking for odd to false
if is looking for even and number is even
distance = remembered position - current position
quit loop
你好像误解了distance = e - o
的意思。这并不意味着 distance
会根据 e
和 o
而改变。这只是意味着 e
和 o
目前持有的任何值都用于计算 distance
的值,并且 distance
将具有相同的值,直到您更改它。在该行之后更改 e
或 o
对 distance
没有影响。因此,distance
将始终为 -1。
您似乎也在使用嵌套循环。这里不需要。这是一个通用算法:
for each element x in the array
if x is odd then
record x's position in y
if an odd number has been found and x is even then
return x's position minus y
这是一些代码:
public static int oddEven(int[] numbers) {
int oddPosition = -1;
for (int i = 0 ; i < numbers.length ; i++) {
if (numbers[i] % 2 == 1) {
oddPosition = i;
}
if (oddPosition != -1 && numbers[i] % 2 == 0) {
return i - oddPosition;
}
}
return -1;
}
查看三行(简化的)代码:
for (int a = 0; a < arrays.length; a++) {
for (int b = a+1; ...) {
array[b]
现在,一旦 a 在数组的末尾并且您执行 b = a+1
,b 在数组的末尾之后,并且执行 array[b]
将抛出异常。
示例:
- 数组 = [1]
- 一=0
- b = 1
- 数组[1] ??? 越界
这是一个解决方案。你不需要 2 个循环
public class App
{
public static void main( String[] args )
{
int arr[] = {3, 5, 7, 1};
System.out.println( go(arr) );
}
static int go(int[] array) {
int oddPos = -1;
for (int i = 0; i < array.length; i++) {
//check if already din't find an odd number and if current number is odd
if (oddPos == -1 && array[i] % 2 == 1) {
oddPos = i;
}
// check if already found an odd number and current number is even
if (oddPos != -1 && array[i] % 2 == 0) {
return i - oddPos;
}
}
return -1;
}
}
问题: 编写一个方法来检查数组中是否有奇数和偶数。找到第一个奇数。然后,在找到奇数之后,开始寻找偶数的位置,然后 return 它们之间的距离。 Return -1 如果没有奇数或没有奇数后跟偶数。
Data:包含 0 个或多个值的数组。该数组不会指向 null。
输出:Return距离或-1。
示例数据:
7、5、10、12
3、5、7、1
2、4、3、8、10、12
11
5、3、11、6、7、11、8
示例输出
2
-1
1
-1
3
这里是对apluscompsci.com的一个问题的介绍。这个问题是在我的 AP Computer Science class 中分配给学生的一个问题。所以,我对这个问题的代码是这样的:
int go( int[] array )
{
int o = 1;
int e = 0;
int distance = e-o;
if(array.length==0)
{
return distance;
}
for(int a = 0, y = 0; a < array.length || y<1; a++)
{
if(array[a]%2==1)
{
y++;
o = a;
for(int b = a + 1, x = 0; b < array.length || x<1; b++)
{
if(array[b]%2==0)
{
x++;
e = b;
}
}
}
}
if(e==0)
{
o=1;
}
return distance;
}
出于某种原因,我为解决问题而构建的代码不断触发 "java.lang.ArrayIndexOutOfBoundsException" 错误。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at Submission.go(typed.java:30)
at Submission.main(typed.java:9)
不过,这是不可能的,因为我已经以最抽象的方式编写了这个解决方案,而且我的 for 循环从不请求不存在的索引.即使是老师或房间里最聪明的孩子也无法帮助我找出答案,我已经调整了这段代码将近 3 天。我已经尝试通过在 Eclipse 中重建代码来进行故障排除,以防本网站的 JavaScript 可能不支持我使用的一些额外技术,例如我的 for 循环中的多变量启动和 or 运算符,但它吐出同样的错误。
另外,这是我在这个网站上的第一个问题!我其实只是注册了一个帐户来问这个,哈哈!希望我把所有内容都简单明了 - 在此先感谢您的帮助!
这是一个伪代码解决方案
for each number in array
if is looking for odd and number is odd
remember position
set is looking for odd to false
if is looking for even and number is even
distance = remembered position - current position
quit loop
你好像误解了distance = e - o
的意思。这并不意味着 distance
会根据 e
和 o
而改变。这只是意味着 e
和 o
目前持有的任何值都用于计算 distance
的值,并且 distance
将具有相同的值,直到您更改它。在该行之后更改 e
或 o
对 distance
没有影响。因此,distance
将始终为 -1。
您似乎也在使用嵌套循环。这里不需要。这是一个通用算法:
for each element x in the array
if x is odd then
record x's position in y
if an odd number has been found and x is even then
return x's position minus y
这是一些代码:
public static int oddEven(int[] numbers) {
int oddPosition = -1;
for (int i = 0 ; i < numbers.length ; i++) {
if (numbers[i] % 2 == 1) {
oddPosition = i;
}
if (oddPosition != -1 && numbers[i] % 2 == 0) {
return i - oddPosition;
}
}
return -1;
}
查看三行(简化的)代码:
for (int a = 0; a < arrays.length; a++) {
for (int b = a+1; ...) {
array[b]
现在,一旦 a 在数组的末尾并且您执行 b = a+1
,b 在数组的末尾之后,并且执行 array[b]
将抛出异常。
示例:
- 数组 = [1]
- 一=0
- b = 1
- 数组[1] ??? 越界
这是一个解决方案。你不需要 2 个循环
public class App
{
public static void main( String[] args )
{
int arr[] = {3, 5, 7, 1};
System.out.println( go(arr) );
}
static int go(int[] array) {
int oddPos = -1;
for (int i = 0; i < array.length; i++) {
//check if already din't find an odd number and if current number is odd
if (oddPos == -1 && array[i] % 2 == 1) {
oddPos = i;
}
// check if already found an odd number and current number is even
if (oddPos != -1 && array[i] % 2 == 0) {
return i - oddPos;
}
}
return -1;
}
}