我已经有一个列表类型整数值,并且我想从索引零开始顺序测试,如果一个元素范围的总和满足特定值,然后将该范围复制到列表中并将其存储在链表列表中。然后再次依次测试,但是现在从前一个范围的下一个索引开始测试,所以如果前一个范围是索引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