简体   繁体   English

清除所有阵列列表数据

[英]Clear all array list data

Why doesn't the code below clear all array list data? 为什么下面的代码没有清除所有数组列表数据?

        Console.WriteLine("Before cleaning:" + Convert.ToString(ID.Count));
        //ID.Count = 20
        for (int i = 0; i < ID.Count; i++)
        {
            ID.RemoveAt(i);
        }
        Console.WriteLine("After cleaning:" + Convert.ToString(ID.Count));
        //ID.Count = 10

Why is 10 printed to the screen? 为什么10张印在屏幕上?

Maybe there is another special function, which deletes everything? 也许有另一个特殊功能,删除一切?

You're only actually calling RemoveAt 10 times. 你实际上只是调用了RemoveAt 10次​​。 When i reaches 10, ID.Count will be 10 as well. i达到10时, ID.Count也将是10。 You could fix this by doing: 可以通过以下方式解决此问题

int count = ID.Count;
for (int i = 0; i < originalCount; i++)
{
    ID.RemoveAt(0);
}

This is an O(n 2 ) operation though, as removing an entry from the start of the list involves copying everything else. 这是一个O(n 2 )操作,因为从列表的开头删除一个条目涉及复制其他所有内容。

More efficiently (O(n)): 更有效率(O(n)):

int count = ID.Count;
for (int i = 0; i < originalCount; i++)
{
    ID.RemoveAt(ID.Count - 1);
}

or equivalent but simpler: 或同等但更简单:

while (ID.Count > 0)
{
    ID.RemoveAt(ID.Count - 1);
}

But using ID.Clear() is probably more efficient than all of these, even though it is also O(n). 但是使用ID.Clear()可能比所有这些更有效,即使它也是O(n)。

`Array.Clear()` 

removes all items in the array. 删除数组中的所有项目。

`Array.RemoveAt(i)` 

removes the element of ith index in the array. 删除数组中第i个索引的元素。

ArrayList.Clear Method
Removes all elements from the ArrayList.

for more detail : http://msdn.microsoft.com/en-us/library/system.collections.arraylist.clear.aspx 有关更多详细信息: http//msdn.microsoft.com/en-us/library/system.collections.arraylist.clear.aspx

Use the clear() Method 使用clear()方法

or 要么

change ID.RemoveAt(i); 改变ID.RemoveAt(i); to ID.RemoveAt(0); ID.RemoveAt(0);

Whenever an element is removed from the collection, its index also changes. 每当从集合中移除元素时,其索引也会发生变化。 Hence when you say ID.RemoveAt(0); 因此当你说ID.RemoveAt(0); the element at index 1 now will be moved to index 0. So again you've to remove the same element (like dequeuing). 索引1处的元素现在将被移动到索引0.所以你再次删除相同的元素(如出队)。 until you reach the last element. 直到你到达最后一个元素。 However if you want to remove all the elements at once you can better use the Clear() method. 但是,如果要一次删除所有元素,可以更好地使用Clear()方法。

After removing 10 items, ID.Count() == 10 and i == 10 so the loop stops. 删除10个项目后, ID.Count() == 10i == 10以便循环停止。

Use ID.Clear() to remove all items in the array list. 使用ID.Clear()删除数组列表中的所有项。

Your code does: 你的代码做了:

ID.RemoveAt(0);
...
ID.RemoveAt(9);
ID.RemoveAt(10); \\ at this point you have already removed 10 
                 \\ items so there is nothing left on 10- 19, but you are left with 
                 \\ the 'first' 10 elements
...
ID.RemoveAt(19);

Generally speaking your method removes every second element from the list.. 一般来说,您的方法会从列表中删除每个第二个元素。

Use ArrayList.Clear instead as other have mentioned. 正如其他人提到的那样使用ArrayList.Clear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM