在C#.NET中是否保证了arraylist的顺序?

时间:2023-01-29 15:29:18

If I'm using an ArrayList in C#.NET, is the order guaranteed to stay the same as the order I add items to it?

如果我在C#.NET中使用ArrayList,订单是否保证与我向其添加项目的顺序保持一致?

5 个解决方案

#1


39  

Yes, elements are always added to the end (unless you specify otherwise, e.g. with a call to Insert). In other words, if you do:

是的,元素总是添加到最后(除非您另行指定,例如调用Insert)。换句话说,如果你这样做:

int size = list.Count;
int index = list.Add(element);
Assert.AreEqual(size, index); // Element is always added at the end
Assert.AreEqual(element, list[index]); // Returned index is position in list

The position will change if you remove any earlier elements or insert new elements ahead of it, of course.

如果您删除任何早期元素或在其前面插入新元素,位置将会改变。

Is there any good reason for you to use ArrayList rather than List<T> by the way? Non-generic collections are so 2003...

顺便说一句,你有没有什么理由让你使用ArrayList而不是List ? 2003年的非通用收藏品......

(The order is stable in List<T> as well, by the way.)

(顺便说一句,List 中的顺序也是稳定的。)

#2


6  

Yes, it is, unless some piece of your code changes the order by e.g. swapping.

是的,除非您的某些代码更改了订单,例如,交换。

#3


4  

Yes it is. Since it's stored as an array.

是的。因为它存储为数组。

Other properties are

其他属性是

  • Guaranteed order
  • 保证订单
  • Random access. You can access any element by index in O(1)
  • 随机访问。您可以通过O(1)中的索引访问任何元素
  • Slow insert and delete in the beginning and middle.
  • 在开头和中间缓慢插入和删除。
  • Unsorted. (Sorting should take O(n log n) using quicksort or similar)
  • 未排序。 (排序应使用快速排序或类似的O(n log n))

#4


2  

Yes. [silly answer length limit]

是。 [傻回答长度限制]

#5


0  

When you add an item to a ArrayList the item will always stay at that index. Unless of course if you change it.

将项添加到ArrayList时,该项将始终保留在该索引处。当然,除非你改变它。

(the framework might rearrange the memory but your index will always stay the same)

(框架可能会重新安排内存,但您的索引将始终保持不变)

#1


39  

Yes, elements are always added to the end (unless you specify otherwise, e.g. with a call to Insert). In other words, if you do:

是的,元素总是添加到最后(除非您另行指定,例如调用Insert)。换句话说,如果你这样做:

int size = list.Count;
int index = list.Add(element);
Assert.AreEqual(size, index); // Element is always added at the end
Assert.AreEqual(element, list[index]); // Returned index is position in list

The position will change if you remove any earlier elements or insert new elements ahead of it, of course.

如果您删除任何早期元素或在其前面插入新元素,位置将会改变。

Is there any good reason for you to use ArrayList rather than List<T> by the way? Non-generic collections are so 2003...

顺便说一句,你有没有什么理由让你使用ArrayList而不是List ? 2003年的非通用收藏品......

(The order is stable in List<T> as well, by the way.)

(顺便说一句,List 中的顺序也是稳定的。)

#2


6  

Yes, it is, unless some piece of your code changes the order by e.g. swapping.

是的,除非您的某些代码更改了订单,例如,交换。

#3


4  

Yes it is. Since it's stored as an array.

是的。因为它存储为数组。

Other properties are

其他属性是

  • Guaranteed order
  • 保证订单
  • Random access. You can access any element by index in O(1)
  • 随机访问。您可以通过O(1)中的索引访问任何元素
  • Slow insert and delete in the beginning and middle.
  • 在开头和中间缓慢插入和删除。
  • Unsorted. (Sorting should take O(n log n) using quicksort or similar)
  • 未排序。 (排序应使用快速排序或类似的O(n log n))

#4


2  

Yes. [silly answer length limit]

是。 [傻回答长度限制]

#5


0  

When you add an item to a ArrayList the item will always stay at that index. Unless of course if you change it.

将项添加到ArrayList时,该项将始终保留在该索引处。当然,除非你改变它。

(the framework might rearrange the memory but your index will always stay the same)

(框架可能会重新安排内存,但您的索引将始终保持不变)