"ArrayIndexOutOfBoundsException" error while trying to add two int arrays

I am trying to implement the "add" elements of two arrays in Java. I have two arrays that contain integers, and I want to add them. I do not want to use immutable variables. I prefer to do this: a.plus (b); The problem is when I add 2 arrays with different lengths. He tries to add elements b to a, but if b is long, he throws an "ArrayIndexOutOfBoundsException" error. I can understand why this is happening. But how can I solve this? How can I expand the array a?: /

public void plus(int[] b) { int maxlength = Math.max( this.length, b.length ); if (maxlength==a.length) { for (int i = 0; i <= maxlength; i++) { a[i] = a[i] + b[i]; //ArrayIndexOutOfBoundsException error } } } 
+4
source share
5 answers

How about this:

 private int[] a; /** * Adds the specified array to our array, element by element, ie * for index i, a[i] = a[i] + b[i]. If the incoming array is * longer, we pad our array with 0 to match the length of b[]. * If our array is longer, then only the first [b.length] values * of our array have b[] values added to them (which is the same * as if b[] were padded with 0 to match the length of a[]. * * @param b the array to add, may not be null */ public void plus(final int[] b) { assert b != null; if (a.length < b.length) { // Expand a to match b // Have to move a to a larger array, no way to increase its // length "dynamically", ie in place. final int[] newA = new int[b.length]; System.arraycopy(a, 0, newA, 0, a.length); // remaining new elements of newA default to 0 a = newA; } for (int i = 0; i < b.length; i++) { a[i] = a[i] + b[i]; } } 

Another version:

 private ArrayList<Integer> aList; public void plusList(final int[] b) { assert b != null; if (aList.size() < b.length) { aList.ensureCapacity(b.length); } for (int i = 0; i < b.length; i++) { if (i < aList.size()) { aList.set(i, aList.get(i) + b[i]); } else { aList.add(b[i]); } } } 

Edit: here is the full class with a sample run from the data in the comments

 public class AddableArray { private int[] a; public AddableArray(final int... a) { this.a = a; } /** * Adds the specified array to our array, element by element, ie * for index i, a[i] = a[i] + b[i]. If the incoming array is * longer, we pad our array with 0 to match the length of b[]. * If our array is longer, then only the first [b.length] values * of our array have b[] values added to them (which is the same * as if b[] were padded with 0 to match the length of a[]. * * @param b the array to add, may not be null */ public void plus(final int[] b) { assert b != null; if (a.length < b.length) { // Expand a to match b // Have to move a to a larger array, no way to increase its // length "dynamically", ie in place. final int[] newA = new int[b.length]; System.arraycopy(a, 0, newA, 0, a.length); // remaining new elements of newA default to 0 a = newA; } for (int i = 0; i < b.length; i++) { a[i] = a[i] + b[i]; } } int[] get() { return a; } @Override public String toString() { final StringBuilder sb = new StringBuilder("a[] = [ "); for (int i = 0; i < a.length; i++) { if (i > 0) sb.append(", "); sb.append(a[i]); } sb.append(" ]"); return sb.toString(); } public static void main (final String[] args) { final AddableArray myAddableArray = new AddableArray(1,2,3); System.out.println("Elements before plus(): "); System.out.println(myAddableArray.toString()); final int b[]={1,2,3,4}; myAddableArray.plus(b); System.out.println("Elements after plus(): "); System.out.println(myAddableArray.toString()); } } 

Run Example:

 Elements before plus(): a[] = [ 1, 2, 3 ] Elements after plus(): a[] = [ 2, 4, 6, 4 ] 
+2
source

i <= maxlength replace this with i < maxlength .

The array index starts from zero, not from one. Thus, the length of the array is one less than the final index of the array. When you use <=, you are trying to jump to one element after the last element in your array. Therefore, this is an exception.

Also you need to check the length of array b. If the length of the array b is less than a, you will encounter the same exception.

int maxlength = Math.min( this.length, b.length ); more suitable.

Or, if you do not want to skip any elements in any of the arrays when adding, an ArrayList is the answer for you. ArrayList is the self-expanding array you are looking for. Here is how you can do it -

  // First ArrayList ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); a.add(2); a.add(3); // Second ArrayList ArrayList<Integer> b = new ArrayList<Integer>(); b.add(1); b.add(2); b.add(3); b.add(4); int maxlength = Math.max(a.size(), b.size()); // Add the elements and put them in the first ArrayList in the corresponding // position for (int i = 0; i < maxlength; i++) { if (i < a.size()) { if (i < b.size()) { int j = a.get(i); a.set(i, j + b.get(i)); } } else { a.add(i, b.get(i)); } } for (int j : a) { System.out.println(j); } 
+8
source

How can I expand array a?

Do not use arrays if you need variable sized data structures. Use List s .

+3
source

maxlength is the maximum size between size [] and b [], so in a loop from 0 to maxlength you will get an ArrayIndexOutOfBoundsException exception when I exceed the min of size [] and b [].

Try the following:

 public void plus(int[] b) { Polynomial a = this; int[] c; int maxlength; if (a.length>b.length) { c=a; maxlength=a.length; } else { c=b; maxlength=b.length; } int ca, cb; for (int i = 0; i < maxlength; i++) { if (i<this.length) ca=a[i]; else ca=0; if (i<b.length) cb=b[i]; else cb=0; c[i] = ca + cb; } } 
+2
source

Try replacing:

 for (int i = 0; i <= maxlength; i++) 

with:

 for (int i = 0; i < maxlength; i++) 
+1
source

All Articles