Reputation: 115
I'm trying to make it so the elements of each array are added together to get a sum, regardless if one array is larger than the other. This is the error I'm getting
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
The error is linked to line 13 or 23 depending on which array is bigger in my test class.
( pickedList[i] = listA[i] + listB[i];
are the error lines)
Edit: The code works if the arrays have the same number of elements, but when one is larger it crashes.
public static int[] AddArray(int[] listA, int[] listB)
{
int aLen = listA.length;
int bLen = listB.length;
int[] pickedList = null;
//if array 1 is longer, make picklist have same number of elements then add
//both array elements together
if (aLen >= bLen)
{ pickedList = new int[aLen];
for( int i = 0; i < aLen; i ++)
{
pickedList[i] = listA[i] + listB[i];
}
}
//if array 2 is longer, make picklist have same number of elements then add
//both array elements together
else
{
pickedList = new int[bLen];
for( int i = 0; i < bLen; i ++)
{
pickedList[i] = listA[i] + listB[i] ;
}
}
return pickedList;
}
}
Upvotes: 0
Views: 89
Reputation: 6515
try this:
public static int[] AddArray(int[] listA, int[] listB)
{
int aLen = listA.length;
int bLen = listB.length;
int[] pickedList = null;
int i,j;
//if array 1 is longer, make picklist have same number of elements then add
//both array elements together
if (aLen >= bLen)
{ pickedList = new int[aLen];
for( i = 0; i < bLen; i ++)
{
pickedList[i] = listA[i] + listB[i];
}
for( j = i; j < aLen; j++) // listB exhaust so add remaining elements of listA to pickedList
{
pickedList[j] = listA[j] ;
}
}
//if array 2 is longer, make picklist have same number of elements then add
//both array elements together
else
{
pickedList = new int[bLen];
for( i = 0; i < aLen; i ++)
{
pickedList[i] = listA[i] + listB[i] ;
}
for( j = i; j < bLen; j ++)// listA exhaust so add remaining elements of listB to pickedList
{
pickedList[j] = listB[j];
}
}
return pickedList;
}
Upvotes: 0
Reputation: 12039
If two arrays are
arr1 = [1, 2, 3, 4, 5]
arr2 = [6, 7, 8]
The we can use lower length of two arrays as loop condition (ind < lower_length
) to get rid from exception. eg,
int array1_length = arr1.length; // 5
int array2_length = arr1.length; // 3
Then we can get lower limit such ways
int limit = (array1_length > array2_length) ? array2_length : array1_length;
or
int limit = Math.max(array1_length, array2_length);
Then can use the limit in for
loop, eg,
for(int ind = 0; ind < limit; ind++){
//sum = arr1[ind] + arr2[ind];
}
I hope it will help you deeply.
Upvotes: 0
Reputation: 1024
public static int[] AddArray(int[] listA, int[] listB)
{
int smallList[], bigList[], sums[];
int pos;
if (listA.length >= listB.length) {
smallList = listB;
bigList = listA;
} else {
smallList = listA;
bigList = listB;
}
sums = new int[bigList.length];
for (pos=0; pos < smallList.length; ++pos) {
sums[pos] = smallList[pos] + bigList[pos];
}
for (; pos < bigList.length; ++pos) {
sums[pos] = bigList[pos];
}
return sums;
}
Upvotes: 0
Reputation: 57
For your first if statement you have aLen >= bLen
Then you loop for the length of aLen in a for loop. Inside this for loop, you try to access the elements of listB at index i. However, since listA is longer, the element listB[i]
will not exist as the length of listA is longer than the length of listB.
Upvotes: 1
Reputation: 201429
I would use Math.max(int,int)
to get the max length. Then declare the new array, then iterate the length adding the elements like
public static int[] addArray(int[] listA, int[] listB) {
int aLen = listA.length;
int bLen = listB.length;
int len = Math.max(aLen, bLen);
int[] pickedList = new int[len];
for (int i = 0; i < len; i++) {
if (i < aLen && i < bLen) {
pickedList[i] = listA[i] + listB[i];
} else if (i < aLen) {
pickedList[i] = listA[i];
} else {
pickedList[i] = listB[i];
}
}
return pickedList;
}
Upvotes: 1
Reputation: 8338
Your error is very simple:
if (aLen >= bLen)
{ pickedList = new int[aLen];
for( int i = 0; i < aLen; i ++)
{
pickedList[i] = listA[i] + listB[i];
you have that.
If alen is longer than blen, then if your forloop goes up to the length of a, you'll get an error because you have listB[i] - you're trying to access elements of B that just aren't there.
Let me make this clearer. Let's say array a has a length of 5, and array b has a length of 3. a is bigger than b, so you loop through i from 0 to 5. Everything will be fine fine for i = 0, i = 1, i = 2, but once you get to i = 3, there is no listB[i], because list B only has an elemnet in the 0, 1, and 2 position, so you get the error that you got.
I hope that helps. Good luck :)
Upvotes: 1