Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

computer science - My Java code is giving me an out of bounds error

How can I find out why this keeps giving me out of bounds errors?

If anyone can, could someone give me advice on how to shorten the the code and make it more efficient? This is for my Computer Science II APA class.

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);
}

Assignment:

/* 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;
 }

Tests

public static void main(String[] args) {
    int[] one = new int[]{1, 2, 3, 4, 5, 6};
    int[] two = new int[]{-1, 3, -4, 8, -5, 6, 7};
    System.out.println(Arrays.toString(one) + " << >> " + Arrays.toString(two));
    System.out.println("maximusArray: " + Arrays.toString(fun(one,two))); // [1, 3, 3, 8, 5, 6, 7]
    System.out.println();

    one = new int[]{-13, -14, -15, 16};
    two = new int[]{};
    System.out.println(Arrays.toString(one) + " << >> " + Arrays.toString(two));
    System.out.println("maximusArray: " + Arrays.toString(fun(one,two)));// {0, 0, 0, 16}
    System.out.println();

    one = new int[]{11, 22, 33};
    two = new int[]{-5, 55, 41, -30, 13};
    System.out.println(Arrays.toString(one) + " << >> " + Arrays.toString(two));
    System.out.println("maximusArray: " + Arrays.toString(fun(one,two)));// {11, 55, 41, 0, 13}
    System.out.println();

}
}

The error message is: ArrayIndexOutOfBoundsException: 6 at arrays15.fun(Arrays_Lab15_maximusArrays.java:33) at arrays15.main(Arrays_Lab15_maximusArrays.java:52)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Out-of-bound errors means you're probably not accessing indices right, so check which of your data structures are vulnerable to that.
  2. Study the error message you get because that at least shows the line where the error happens (would have been better if it was included in the post).

It seems the second for-loop in the fun method is causing the error:

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

Why is nums1 being referenced instead of nums2?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...