List <string> .remove似乎无法正常工作</string>

时间:2014-01-06 22:59:43

标签: java

我在Windows 7上的Netbeans IDE 7.4上使用Java进行开发。我使用以下代码逐步删除和移动字符串列表中的元素。

static void RemoveSubsequence(List<String> entries, int start, int end){
    int removeIndex=start+1;
    String debugStr;
    for (int i=removeIndex; i<end; ++i){
        debugStr=entries.remove(removeIndex);
    } 
}

以下是以索引removeIndex开头的列表中13个元素的结果。第一列显示初始值,而接下来的两列显示前两次迭代的结果。第一次迭代完美地工作,但第二次迭代将应该是624(第二行)更改为634.

634 624 624
624 624 634
624 624 634
624 634 624
634 624 634
624 634 624
634 624 634
624 634 776
634 776 672
776 672 624
672 624 776
624 776 776
776 776 672

2 个答案:

答案 0 :(得分:2)

我想我明白你打算做什么,抱歉。

如果你想删除列表的一个子集,你应该做的是继续删除索引“开始”的项目,并删除相同的索引“结束 - 开始”时间。

int times = end - start;

For(int i = 0; i&lt; times; i ++) {    String listItem = list.remove(start); }

现在,您的列表已删除了所有必需的项目。

答案 1 :(得分:0)

我首先从List中删除该条目,然后分配debugStr的字符串值。看看是否有帮助。