Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Java代码给了我一个越界错误_Java_Computer Science - Fatal编程技术网

我的Java代码给了我一个越界错误

我的Java代码给了我一个越界错误,java,computer-science,Java,Computer Science,我怎样才能找出为什么这会不断给我带来越界错误 如果有人可以的话,有人能给我一些建议,如何缩短代码的长度,提高效率?这是我的计算机科学II APA课程 import java.util.*; class arrays15 { /* write a method that will find the largest values * between two ints. */ public static int large(int num1, int num2) { return Ma

我怎样才能找出为什么这会不断给我带来越界错误

如果有人可以的话,有人能给我一些建议,如何缩短代码的长度,提高效率?这是我的计算机科学II APA课程

import java.util.*;
class arrays15
{

/* write a method that will find the largest values
 *  between two ints.
 */
public static int large(int num1, int num2) {
    return Math.max(num1, num2);
}
任务:

/* fun will take two int arrays and for each position
 * it will compare the two values from each array and put the
 * larger of the two in a new array. If there are not two values
 * to compare then take then assume the other value is a zero (0).
 *  
 * Must call another method to find largest.
 */

 public static int[] fun(int[] nums1, int[] nums2) {



    ArrayList<Integer> a = new ArrayList<Integer>();
    ArrayList<Integer> b = new ArrayList<Integer>();
    for(int g = 0; g < nums1.length; g++) {
        a.add(nums1[g]);
    }
    for(int h = 0; h < nums2.length; h++) {
        b.add(nums1[h]);
    }
    for(int i = a.size(); i < b.size(); i++) {
        a.add(0);
    }
    for(int i = b.size(); i < a.size(); i++) {
        b.add(0);
    }
    int[] r = new int[a.size()];
    for(int j = 0; j < r.length; j++) {
        r[j] = large(a.remove(0), b.remove(0));
    }
    return r;
 }
/*fun将为每个位置使用两个int数组和
*它将比较每个数组中的两个值,并将
*新阵列中两个中较大的一个。如果没有两个值
*要进行比较,请假定另一个值为零(0)。
*  
*必须调用另一个方法才能找到最大值。
*/
公共静态int[]fun(int[]nums1,int[]nums2){
ArrayList a=新的ArrayList();
ArrayList b=新的ArrayList();
对于(int g=0;g
测验

publicstaticvoidmain(字符串[]args){
int[]one=新的int[]{1,2,3,4,5,6};
int[]two=新的int[]{-1,3,-4,8,-5,6,7};
System.out.println(Arrays.toString(一)+“>”+Arrays.toString(二));
System.out.println(“maximusArray:+array.toString(fun(一,二));//[1,3,3,8,5,6,7]
System.out.println();
一=新的int[]{-13,-14,-15,16};
二=新的int[]{};
System.out.println(Arrays.toString(一)+“>”+Arrays.toString(二));
System.out.println(“maximusArray:+array.toString(fun(one,two));//{0,0,0,16}
System.out.println();
一=新的int[]{11,22,33};
二=新的整数[]{-5,55,41,-30,13};
System.out.println(Arrays.toString(一)+“>”+Arrays.toString(二));
System.out.println(“maximusArray:+array.toString(fun(一,二));//{11,55,41,0,13}
System.out.println();
}
}
错误消息是: ArrayIndexOutofBounds异常:6 在arrays15.fun(Arrays_Lab15_maximusArrays.java:33) 在arrays15.main(Arrays_Lab15_maximusArrays.java:52)

  • 越界错误意味着您可能没有正确访问索引,因此请检查哪些数据结构容易受到此问题的影响
  • 研究你收到的错误消息,因为它至少显示了错误发生的那一行(如果它包含在文章中会更好)
  • fun方法中的第二个for循环似乎导致了错误:

    for(int h = 0; h < nums2.length; h++) {
        b.add(nums1[h]);
    }
    
    for(int h=0;h

    为什么引用的是
    nums1
    而不是
    nums2

    哪一行给出了越界错误?请编辑你的帖子,将其包括在内。我会敦促你改进你的帖子——诚然答案是肯定的,但你已经有三张接近票数的选票了,只需要再投两张。我们需要这些细节,让这个问题对你以外的读者有用。这看起来很有帮助,但是如果你还没有足够的信息来回答,最好等到你回答。如果OP现在没有添加澄清,您将得到一个包含永久澄清请求的答案。谢谢您,M.M。!你的回答对我帮助很大!它解决了我的问题。谢谢大家!@欢迎并希望它对未来有所帮助too@halfer你说得对。我只是想增加我的知名度,这样我就可以做一些基本的事情。下一个目标是50,所以我可以发表评论。在那之前,我将一直生活在边缘地带:-)
    for(int h = 0; h < nums2.length; h++) {
        b.add(nums1[h]);
    }