我是高中的计算机科学专业的学生,我得到了一个项目,在这个项目中,我必须创建一个程序,可以添加整数数组的值,计算数组中包含的值的数量,并删除数组中每个值的实例。
下面是代码,分为上面描述的每个函数的三种方法:
import java.lang.System;
import java.lang.Math;
import java.util.Arrays;
public class ArrayFunHouse
{
//instance variables and constructors could be used, but are not really needed
//getSum() will return the sum of the numbers from start to stop, not including stop
public static int getSum(int[] numArray, int start, int stop)
{
int count = start;
int output = 0;
while(count<=stop)
{
output = output + numArray[count];
count++;
}
return output;
}
//getCount() will return number of times val is present
public static int getCount(int[] numArray, int val)
{
int x = 0;
int count = 0;
while(x<numArray.length)
{
if(val==numArray[x])
count++;
x++;
}
return count;
}
public static int[] removeVal(int[] numArray, int val)
{
int[] newArray = new int[ numArray.length - getCount(numArray, val) ];
int x = 0;
for(int position = 0; position < numArray.length; position++)
{
x = numArray[position];
if(x!=val)
{
newArray[position] = numArray[position];
}
}
return newArray;
}
}下面是设计用于执行代码的运行程序,包括指示我们使用的示例数据:
import java.util.Arrays;
public class ArrayFunHouseRunner
{
public static void main( String args[] )
{
int[] one = {7, 4, 10, 0, 1, 7, 6, 5, 3, 2, 9, 7};
ArrayFunHouse test = new ArrayFunHouse();
System.out.println(Arrays.toString(one));
System.out.println("sum of spots 3-6 = " + ArrayFunHouse.getSum(one,3,6));
System.out.println("sum of spots 2-9 = " + ArrayFunHouse.getSum(one,2,9));
System.out.println("# of 4s = " + ArrayFunHouse.getCount(one,4));
System.out.println("# of 9s = " + ArrayFunHouse.getCount(one,9));
System.out.println("# of 7s = " + ArrayFunHouse.getCount(one,7));
System.out.println("new array with all 7s removed = " + test.removeVal(one,7));
System.out.println("# of 7s = " + ArrayFunHouse.getCount(ArrayFunHouse.removeVal(one,7),7));
int[] two = {4,2,3,4,6,7,8,9,0,10,0,1,7,6,5,3,2,9,9,8,7};
//add test cases
}
}当我运行代码时,输出如下:
[7, 4, 10, 0, 1, 7, 6, 5, 3, 2, 9, 7]
sum of spots 3-6 = 14
sum of spots 2-9 = 34
# of 4s = 1
# of 9s = 1
# of 7s = 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at ArrayFunHouse.removeVal(ArrayFunHouse.java:49)
at ArrayFunHouseRunner.main(ArrayFunHouseRunner.java:21)
Process completed.如上所示,代码运行平稳,直到到达第三种方法。如错误消息所示,要使代码顺利运行,需要修复什么?
发布于 2017-01-12 03:26:27
在removeVal函数中,您试图将新数组的索引设置为与旧数组中的索引相同。当数组变小时,它不能将键9放在一个8项数组中。
将其更改为:
int newPosition = 0;// outside of loop
x = numArray[position];
if(x!=val)
{
newArray[newPosition] = numArray[position];
newPosition++;
}发布于 2017-01-12 03:27:08
for(int position = 0; position < numArray.length; position++)
{
x = numArray[position];
if(x!=val)
{
newArray[position] = numArray[position];
}
}不能使用position同时访问目标数组和源数组。您需要两个变量,如果是x == val,其中一个变量不会增加。
发布于 2017-01-12 03:27:49
我想是因为这句话:newArray[position] = numArray[position];。因为newArray比numArray短,所以numArray的位置指数超出了numArray的范围。您可能需要两个位置值,比如"newArPos“和"numArPos",如果排除了一个值,则不增加"newArPos”。
https://stackoverflow.com/questions/41604553
复制相似问题