Java Jgrasp中的阵列不反弹
编写几种数组操作方法。其中一种方法将总结一个问题的一部分 如果提供数组,则另一个方法将计算数组中出现的特定数字的数量,以及最后一个 方法将从数组中删除所有特定值 我已经完成了代码,但是我没有跳出bounce:9错误,我无法解决它Java Jgrasp中的阵列不反弹,java,arrays,output,Java,Arrays,Output,编写几种数组操作方法。其中一种方法将总结一个问题的一部分 如果提供数组,则另一个方法将计算数组中出现的特定数字的数量,以及最后一个 方法将从数组中删除所有特定值 我已经完成了代码,但是我没有跳出bounce:9错误,我无法解决它 导入java.lang.System; 导入java.lang.Math; 公共班级宿舍{ //可以使用实例变量和构造函数,但实际上不是 //需要 //getSum()将返回从开始到停止的数字之和,而不是 //包括停止 公共静态int getSum(int[]numAr
导入java.lang.System;
导入java.lang.Math;
公共班级宿舍{
//可以使用实例变量和构造函数,但实际上不是
//需要
//getSum()将返回从开始到停止的数字之和,而不是
//包括停止
公共静态int getSum(int[]numArray,int start,int stop)
{
整数和=0;
int[]locArray=numArray;
对于(int i=start;i在这行代码中:ArrayFunHouse.getSum(one,3,16));
,将停止索引作为16传递给第一个数组,需要做的是将其传递给第二个数组two。这是因为第一个数组的长度小于16。此外,在其他一些代码行中,您传递了一行,而不是两行。请尝试查看它是否有效 问题的措辞似乎表明这是某种家庭作业,堆栈溢出不是家庭作业帮助的地方,这是老师的工作。复制/粘贴错误:getSum(one,3,16)
应该是getSum(two,3,16)
。你忘了更改第一个参数。@KrishnanshuGupta。虽然我同意这个问题可以简化为一个更简洁的问题。@iled,我到底错在哪里,我不明白我所说的什么是错的?尽管根据meta,它说:询问你现有实现的具体问题。如果可以的话现在还不要这样做,先尝试一下你自己的工作,或者寻求更全面的帮助;在这个阶段,你的教授可能是比堆栈溢出更好的资源。承认这个问题是家庭作业。试图隐藏它只会更快地解决问题。不要使用“家庭作业”标记,但如果相关,请在问题文本中提及,但他没有做后者,因此我的评论仍然有效。@Abdullah,您介意选择它作为正确答案,以便将来,人们知道问题得到了回答,如果需要,可以更好地引用它,谢谢。
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};
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));
one = ArrayFunHouse.removeVal(one, 7);
System.out.println("new array with all 7s removed = " +
Arrays.toString(one));
System.out.println("# of 7s = " + ArrayFunHouse.getCount(one, 7));
System.out.println();
int[] two = {7, 4, 2, 7, 3, 4, 6, 7, 8, 9, 7, 0, 10, 7, 0, 1, 7, 6, 5, 7, 3, 2, 7, 9, 9, 8,7};
System.out.println(Arrays.toString(one));
System.out.println("sum of spots 3-16 = " +
ArrayFunHouse.getSum(one,3,16));
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));
one = ArrayFunHouse.removeVal(one, 7);
System.out.println("new array with all 7s removed = " +
Arrays.toString(one));
System.out.println("# of 7s = " + ArrayFunHouse.getCount(one, 7));
}
}
[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
new array with all 7s removed = [4, 10, 0, 1, 6, 5, 3, 2, 9]
# of 7s = 0
[4, 10, 0, 1, 6, 5, 3, 2, 9]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at ArrayFunHouse.getSum(ArrayFunHouse.java:22)
at ArrayFunHouseRunner.main(ArrayFunHouseRunner.java:30)