Although there are some similarities between arrays and lists, they are used for different purposes.
An array is a contiguous segment of memory, and a list is just a bunch of nodes, each one has the pointers to the "next" node (and also to the "previous" node, in the case of bidirectional lists).
Arrays efficiently - in O(1) - support random access (i.e. by arbitrary given index i), but deleting/inserting an element into an array is slow - O(n), because you have to move all elements after deleting/inserting element. Lists, on the other hand, do not support efficient random access (while supporting efficient consecutive traversal), but inserting and deleting is fast - O(1).
Look at this picture: : and at this link for a better explanation.
Arrays and lists are different data structures. Arrays are not necessarily ordered.
Performance wise, maintaining an ordered list is pretty expensive: O(N) insert, delete, but you can do searches faster than O(N) (using something like binary search). With a regular array, search is O(N). With arrays, you can do random access of a member in O(1), while this takes O(N) in an list.
The items in an array are not necessarily in any particular order.
In general, it is possible to add items to a particular point in the list more swiftly than it is possible to add a new item in an array at an equivalent point. (In the array, you have to shuffle other entries around; in a list, you simply adjust appropriate pointers in at most 3 elements.) Similarly for deleting elements from lists and arrays.
Access to the Nth item in a list takes O(N) time but is O(1) time for an array.
发布评论
评论(3)
尽管数组和列表之间有一些相似之处,但它们的用途不同。
数组是连续的内存段,列表只是一堆节点,每个节点都有指向“下一个”节点的指针(在双向列表的情况下,还有指向“上一个”节点的指针)。
数组高效 - O(1) - 支持随机访问(即通过任意给定索引 i),但删除/插入元素到数组中的速度很慢 - O(n),因为您必须在删除/插入元素后移动所有元素。
另一方面,列表不支持高效的随机访问(同时支持高效的连续遍历),但插入和删除速度很快 - O(1)。
看这张图片: :
并在此链接获得更好的解释。
Although there are some similarities between arrays and lists, they are used for different purposes.
An array is a contiguous segment of memory, and a list is just a bunch of nodes, each one has the pointers to the "next" node (and also to the "previous" node, in the case of bidirectional lists).
Arrays efficiently - in O(1) - support random access (i.e. by arbitrary given index i), but deleting/inserting an element into an array is slow - O(n), because you have to move all elements after deleting/inserting element.
Lists, on the other hand, do not support efficient random access (while supporting efficient consecutive traversal), but inserting and deleting is fast - O(1).
Look at this picture: :
and at this link for a better explanation.
数组和列表是不同的数据结构。数组不一定是有序的。
就性能而言,维护有序列表非常昂贵:O(N) 插入、删除,但您可以比 O(N) 更快地进行搜索(使用二分搜索之类的东西)。对于常规数组,搜索的时间复杂度为 O(N)。使用数组,您可以在 O(1) 内随机访问成员,而在列表中则需要 O(N)。
Arrays and lists are different data structures. Arrays are not necessarily ordered.
Performance wise, maintaining an ordered list is pretty expensive: O(N) insert, delete, but you can do searches faster than O(N) (using something like binary search). With a regular array, search is O(N). With arrays, you can do random access of a member in O(1), while this takes O(N) in an list.
数组中的项目不一定按任何特定顺序排列。
一般来说,将项目添加到列表中的特定点可能比在数组中的等效点添加新项目更快。 (在数组中,您必须打乱其他条目;在列表中,您只需在最多 3 个元素中调整适当的指针。)类似地,从列表和数组中删除元素。
访问列表中的第 N 项需要 O(N) 时间,但对于数组来说是 O(1) 时间。
The items in an array are not necessarily in any particular order.
In general, it is possible to add items to a particular point in the list more swiftly than it is possible to add a new item in an array at an equivalent point. (In the array, you have to shuffle other entries around; in a list, you simply adjust appropriate pointers in at most 3 elements.) Similarly for deleting elements from lists and arrays.
Access to the Nth item in a list takes O(N) time but is O(1) time for an array.