数组与链表 [英] Array versus linked-list

查看:137
本文介绍了数组与链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有人想在数组中使用链接列表?

Why would someone want to use a linked-list over an array?

编辑链表无疑比使用数组有点多的工作有人可能会怀疑什么才能证明额外的努力。

Coding a linked-list is, no doubt, a bit more work than using an array and one may wonder what would justify the additional effort.

我认为插入新元素在链表中是微不足道的,但它是数组中的主要烦恼。使用链表存储一组数据而不是将其存储在数组中还有其他优点吗?

I think insertion of new elements is trivial in a linked-list but it's a major chore in an array. Are there other advantages to using a linked list to store a set of data versus storing it in an array?

此问题与这个问题,因为另一个问题是特别询问特定的Java类,而这个问题涉及到一般数据结构。

This question is not a duplicate of this question because the other question is asking specifically about a particular Java class while this question is concerned with the general data structures.

推荐答案


  • 将链接列表中不同大小的数据存储起来更为容易。一个数组假定每个元素的大小完全相同。

  • 如上所述,链表可以更容易地有机地发展。数组的大小需要提前知道,或者在需要增长时重新创建。

  • 重新排列链表只是一个改变什么的东西。改组数组更复杂和/或占用更多的内存。

  • 只要您的迭代都发生在foreach上下文中,则不会在迭代中丢失任何性能。

    • It's easier to store data of different sizes in a linked list. An array assumes every element is exactly the same size.
    • As you mentioned, it's easier for a linked list to grow organically. An array's size needs to be known ahead of time, or re-created when it needs to grow.
    • Shuffling a linked list is just a matter of changing what points to what. Shuffling an array is more complicated and/or takes more memory.
    • As long as your iterations all happen in a "foreach" context, you don't lose any performance in iteration.
    • 这篇关于数组与链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆