简体   繁体   中英

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)

  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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM