我已經有一個列表類型整數值,並且我想從索引零開始順序測試,如果一個元素範圍的總和滿足特定值,然後將該範圍複製到列表中並將其存儲在鏈表列表中。然後再次依次測試,但是現在從前一個範圍的下一個索引開始測試,所以如果前一個範圍是索引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清除此代碼失敗,因此沒有任何替代使用具有這種邏輯而不管該數據結構的代碼?
這是一個非常長的第一句話。你在「如果一個範圍的總和......」中失去了我們。 – ergonaut
你甚至更早失去了我。關於何時開始調用「LinkedList」和數組數組。在Java中,數組與列表有不同的類型。如果你將一個「列表」稱爲「數組」,反之亦然,結果將會是我們在「數組」是指Java數組還是Java「List」時計算出來的。 –
@ergonaut我編輯了這篇文章,它是一個元素範圍的總和,如果你看到代碼,例如arrayB中的元素具有{1,2,3,1,1,1,1,2}元素,所以前3個元素的總和爲6,滿足條件(count == n),因此範圍將是索引0到索引2。 – Andres