[英]Error trying to sum two different length arrays
我正在嘗試使每個數組的元素加在一起以獲得總和,而不管一個數組是否大於另一個數組。 這是我得到的錯誤
線程“主”中的異常java.lang.ArrayIndexOutOfBoundsException:5
該錯誤鏈接到第13或23行,具體取決於測試類中哪個數組更大。 ( pickedList[i] = listA[i] + listB[i];
是錯誤行)
編輯 :如果數組具有相同數量的元素,則代碼有效,但是當數組大時,它會崩潰。
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;
}
}
您的錯誤非常簡單:
if (aLen >= bLen)
{ pickedList = new int[aLen];
for( int i = 0; i < aLen; i ++)
{
pickedList[i] = listA[i] + listB[i];
你有那個。
如果alen比blen長,那么如果您的forloop達到a的長度,則會出現錯誤,因為您擁有listB [i]-您正在嘗試訪問不存在的B元素。
讓我說清楚一點。 假設數組a的長度為5,數組b的長度為3。a大於b,因此您從0到5循環遍歷i。對於i = 0,i = 1,i,一切都會很好。 = 2,但是一旦到達i = 3,就沒有listB [i],因為列表B在0、1和2位置僅具有一個elemnet,因此您將得到錯誤。
希望對您有所幫助。 祝好運 :)
我將使用Math.max(int,int)
來獲取最大長度。 然后聲明新數組,然后迭代添加元素的長度,例如
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;
}
對於第一個if語句,您有aLen >= bLen
然后在for循環中循環aLen的長度。 在此for循環內,您嘗試訪問索引i處的listB元素。 但是,由於listA較長,因此元素listB[i]
將不存在,因為listA的長度大於listB的長度。
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;
}
如果兩個數組是
arr1 = [1, 2, 3, 4, 5]
arr2 = [6, 7, 8]
我們可以使用兩個數組的較低長度作為循環條件( ind < lower_length
)來擺脫異常。 例如,
int array1_length = arr1.length; // 5
int array2_length = arr1.length; // 3
這樣我們就可以得到下限
int limit = (array1_length > array2_length) ? array2_length : array1_length;
要么
int limit = Math.max(array1_length, array2_length);
然后可以在for
循環中使用限制,例如,
for(int ind = 0; ind < limit; ind++){
//sum = arr1[ind] + arr2[ind];
}
希望對您有幫助。
嘗試這個:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.