2015-10-17 191 views
0

我已經有一個列表類型整數值,並且我想從索引零開始順序測試,如果一個元素範圍的總和滿足特定值,然後將該範圍複製到列表中並將其存儲在鏈表列表中。然後再次依次測試,但是現在從前一個範圍的下一個索引開始測試,所以如果前一個範圍是索引0到索引9,則從索引10開始,然後重複該過程直到最後一個索引。將一系列列表轉換爲子列表,並將它們存儲在一個鏈表類型列表中

List<Integer> arrayB = new LinkedList<Integer>(); //this is the array with values in it 

List<LinkedList> p = new LinkedList<LinkedList>();// this is the array of arrays 

List<Integer> arrayA = new LinkedList<Integer>();// this is the range or the sub list of arrayB 

public void function(int n)// suppose that n = 6 and arrayB have these value {1,2,3,1,1,1,1,2} 
{ 
    int count = 0; 

    for (int w : arrayB) 
    { 
     count = w + count; 
     arrayA.add(w); 
     if(count == n) 
     { 
      count = 0; 
      p.add((LinkedList) arrayA); 
      arrayA.clear(); 
     } 
    } 
} 

然而,當我調用方法中arrayA清除此代碼失敗,因此沒有任何替代使用具有這種邏輯而不管該數據結構的代碼?

+0

這是一個非常長的第一句話。你在「如果一個範圍的總和......」中失去了我們。 – ergonaut

+0

你甚至更早失去了我。關於何時開始調用「LinkedList」和數組數組。在Java中,數組與列表有不同的類型。如果你將一個「列表」稱爲「數組」,反之亦然,結果將會是我們在「數組」是指Java數組還是Java「List」時計算出來的。 –

+0

@ergonaut我編輯了這篇文章,它是一個元素範圍的總和,如果你看到代碼,例如arrayB中的元素具有{1,2,3,1,1,1,1,2}元素,所以前3個元素的總和爲6,滿足條件(count == n),因此範圍將是索引0到索引2。 – Andres

回答

0

您每次向p添加子列表時都使用相同的列表引用arrayA,p中的每個列表元素都指向相同的arrayA。所以當你調用arrayA.clear();您清除p中的所有列表元素。

要糾正這一點,你需要創建一個新的列表對象當您添加一個子表到arrayA:

public static void function(int n)// suppose that n = 6 and arrayB have these value {1,2,3,1,1,1,1,2} 
{ 
    int count = 0; 

    LinkedList<Integer> subList = new LinkedList<>(); 
    for (int w : arrayB) { 
     count = w + count; 
     subList.add(w); 
     if (count == n) { 
      count = 0; 
      p.add((LinkedList) subList); // p is adding a new list reference every time 
      subList = new LinkedList<>(); // create a new list object, subList points to a new list object 
     } 
    } 
} 
+0

這是我希望它的工作完全正常的答案,我不得不改變一行代碼謝謝。 – Andres

0

我對這個問題的理解是這樣的: 存在從您想一個數組在滿足某些標準的情況下提取一定範圍的值。在這種情況下,標準是範圍評估爲某個總和。完成此操作後,您希望重複該過程,直到原始數據結構中的所有值都已用盡。 我會假設你的原始數據結構是一個整數數組,並且你的結果數據結構是整數數組的鏈表。

一種方式做到這一點可能是保持全局計數器,用於跟蹤原始數組的當前索引,如像下面這樣:

int[] originalArray = {//list of numbers separated by commas}; 
LinkedList<Integer[]> resultingList = new LinkedList<>(); 
int currentIndex = 0; 

public static void function(int totalSum) { 
    int currentSum = 0; 
    int initialIndex = currentIndex; 
    while((currentSum != totalSum) && (currentIndex < (originalArray.length - 1))) { 
     if(currentSum + initialArray[currentIndex] <= totalSum) { 
      currentSum += initialArray[currentIndex]; 
      currentIndex++; 
     } 
     else { 
      break; 
     } 
    } 
    if(currentSum = totalSum) { 
     int[] arrayToAdd = new int[currentIndex - initialIndex - 1]; 
     for(int i = 0; i < currentIndex - initialIndex; i++) { 
      arrayToAdd[i] = originalArray[initialIndex + i]; 
     } 
     resultingList.add(arrayToAdd); 
    } 
} 
+0

其實前面的代碼的問題是我刪除了數組p的元素指向的arrayA對象的引用,所以解決方案只是再次初始化數組而不是調用方法清除 – Andres

+0

好吧,很好,你知道了! –

0

的問題是,當您添加鏈表插入最終存儲器p中,則假定列表中的元素放在那裏。只有一個指針被引用,所以當你清除它的下一行時,所有的元素都消失了。

p.add((LinkedList) arrayA); 
arrayA.clear(); 

一個技巧是將arrayA的範圍移動到函數內部。這是因爲它是臨時的,只有一個子列表,所以它不應該在實例級別。它可以通過做

arrayA = new LinkedList<Integer>(); 

重複使用,這樣做的時候,因爲p被保持對它的引用您還沒有失去舊列表。

另一個技巧是使用有意義的名稱命名您的列表。

originalIntList,groupedIntList,singleGroupIntList幫助讀者弄清楚他們可能做的不僅僅是評論,說明Java對象的明顯方面。

+0

是的,這是代碼的問題,修復非常簡單,下次我會考慮爲代碼編寫有意義的名稱 – Andres

相關問題