适合初学者的数据结构

了解您每天使用的数据结构。 (Get to know the data structures that you use every day. )

Welcome! Let’s Start with some Vital Context. Let me ask you this: ✅ Do you listen to music on your smartphone?✅ Do you keep a list of contacts on your phone?✅ Have you ever seen a leaderboard during a competition?

欢迎! 让我们从一些重要的上下文开始。 让我问您一个问题:you您是否在智能手机上听音乐?✅您是否在手机上保留了联系人列表?✅您在比赛中见过排行榜吗?

If your answer is “yes” to any of these questions, then it’s almost certain that you’ve used arrays and you didn’t even know it! Arrays are very powerful data structures that store lists of elements. They have endless applications. They are very important in the world of computer science.

如果您对以上任何一个问题的回答是“是”,那么几乎可以肯定您已经使用了数组,甚至都不知道! 数组是非常强大的数据结构,用于存储元素列表。 牛逼嘿,有无限的应用。 它们在计算机科学领域非常重要。

In this article, you will learn the pros and cons of arrays, their structure, operations, and use cases.

在本文中,您将学习数组的优缺点,数组的结构,操作和用例。

Let’s begin!

让我们开始!

深入研究数组的基本结构 (Deep Dive Into the Basic Structure of Arrays)

To understand how they work, it’s very helpful to visualize your computer’s memory as a grid, just like the one below. Each piece of information is stored in one of those small elements (squares) that make the grid.

要了解它们是如何工作的,将您的计算机内存显示为网格非常有帮助,就像下面的网格一样。 每条信息都存储在构成网格的那些小元素(正方形)之一中。

Arrays take advantage of this “grid” structure to store lists of related information in adjacent memory locations to guarantee extreme efficiency for finding those values.

阵列利用这种“网格”结构将相关信息列表 存储 相邻的内存位置中,以确保查找那些值的极高效率。

You can think of arrays like this:

您可以想到这样的数组:

Their elements are next to each other in memory. If you need to access more than one of them, the process is extremely optimized because your computer already knows where the value is located.

它们的元素在内存中彼此相邻。 如果您需要访问多个选项中的一个,则该过程将得到极大的优化,因为您的计算机已经知道该值所在。

Awesome, right? Let’s learn how this works behind the scenes!

太好了吧? 让我们了解幕后工作原理!

分类 (Classification)

Arrays are classified as Homogeneous Data Structures because they store elements of the same type.

数组被归类为同类数据结构,因为它们 存储相同类型的元素

They can store numbers, strings, boolean values (true and false), characters, objects, and so on. But once you define the type of values that your array will store, all its elements must be of that same type. You can’t “mix” different types of data.

它们可以存储数字,字符串,布尔值(正确和错误),字符,对象等。 但是, 一旦定义了数组将存储的值的类型,则 其所有元素都必须是同一类型。 您不能“混合”不同类型的数据。

读价值观—魔术开始了! (Reading Values — The Magic Begins!)

The amazing power of arrays comes from their efficiency to access values. This is achieved thanks to its grid-like structure. Let’s take a look at this in more detail.?

数组的强大功能来自于访问值效率 。 这要归功于其类似网格的结构。 让我们更详细地看一下。

When you create an array, you:- Assign it to a variable.- Define the type of elements that it will store.- Define its size (the maximum number of elements).

创建数组时,您: -将其分配给变量。-定义将存储的元素的类型。-定义其大小(元素的最大数量)。

Note: The name that you assign to this variable is very important because you will use it later in your code to access values and to modify the array.

注意:分配给该变量的名称非常重要,因为稍后将在代码中使用它来访问值和修改数组。

But how can you tell the computer which particular value you would like to access? This is where indices take a vital role!

但是,如何告诉计算机您要访问哪个特定值? 这就是索引起着至关重要的作用!

1️⃣指数 (1️⃣ Indices)

You use what it’s called an “index” (“indices” in plural) to access a value in an array. This is a number that refers to the location where the value is stored.

您可以使用所谓的“索引” (复数形式的“索引”)来访问数组中的值。 这是一个数字,表示存储值的位置。

As you can see in the diagram below, the first element in the array is referred to using index 0. As you move further to the right, the index increases by one for each space in memory.

如下图所示,使用索引0来引用数组中的第一个元素。当您向右移动时,内存中的每个空间的索引都会增加一个。

Note: I know that it seems strange at first to start counting from 0 instead of 1, but this is called Zero-Based Numbering. It’s very common in computer science.

注意:我知道从0开始而不是从1开始计数似乎很奇怪,但这被称为从零开始的编号 。 这在计算机科学中很常见。

The general syntax to access an element is: <ArrayVariable>[&l]

访问元素的一般语法为: <ArrayVariable>[&l]

For example: If your array is stored in the variable myArray and you want to access the first element (at index 0), you would use myArray[0]

例如:如果您的数组存储在变量myArray并且您要访问第一个元素(索引为0),则可以使用myArray[0]

2️⃣记忆 (2️⃣ Memory)

Now that you know how to access values, let’s see how arrays are stored in your computer’s memory. When you define the size of the array, all of that space in memory is “reserved” from that moment on for future values that you may want to insert.

现在您知道了如何访问值,让我们看看如何将数组存储在计算机内存中。 定义数组的大小时,从那时起“保留”内存中的所有空间,以备将来可能要插入的值。

Note: If you do not fill the array with values, that space will be kept reserved and empty until you do.

注意:如果不使用值填充数组,则该空间将保留并保留为空,直到您这样做。

For Example:Let’s say that you define an array of size 5 but only insert one value. All that remaining space will be empty and “reserved” in memory, waiting for future assignments.

例如:假设您定义了一个大小为5的数组,但仅插入一个值。 所有剩余的空间将为空,并“保留”在内存中,等待将来的分配。

This is key because arrays are extremely efficient in accessing values because all the elements are stored in contiguous spaces in memory. This way, the computer knows exactly where to look to find the information you requested.

这很关键,因为数组非常有效地访问值,因为所有元素都存储在内存中的连续空间中。 这样,计算机就可以准确地找到要查找的信息的位置。

But… there is a downside to it because this is not memory-efficient. You are reserving memory for future operations that may not occur. This is why arrays are recommended in situations when you know beforehand how many elements you are going to store.

但是……它有一个缺点,因为这并不节省内存。 您正在为将来可能不会发生的操作保留内存。 这就是为什么在事先知道要存储多少元素的情况下建议使用数组的原因。

运营-幕后! (Operations — Behind the Scenes!)

Now that you know what arrays are when they are used, and how they store elements, we will dive into their operations like insertion and removal.

既然您知道什么时候使用什么数组,以及它们如何存储元素,我们将深入研究它们的操作,如插入和删除。

1️⃣插入—欢迎光临! (1️⃣ Insertion — Welcome!)

Let’s say that we have an array of size 6 and there’s still an empty space. We want to insert an element “e” at the beginning of the array (index 0), but this place is already taken by the element “a.” What should we do?

假设我们有一个大小为6的数组,仍然有一个空白空间。 我们想在数组的开头(索引0)插入元素“ e”,但是元素“ a”已经占据了这个位置。 我们应该做什么?

To insert into arrays, we move all the elements located to the right of the insertion site, one index to the right. Element “a” will now be at index 1, element “b” will be at index 2 and so on…

为了插入数组 ,我们将所有元素移到插入位置的右边,向右移一个索引。 现在,元素“ a”将位于索引1,元素“ b”将位于索引2,依此类推……

Note: You will need to create a variable to keep track of the last index that contains elements. In the diagram above, the array is filled up to index 4 before the insertion. This way, you can determine if the array is full and what index you should use to insert an element at the end.

注意:您将需要创建一个变量来跟踪包含元素的最后一个索引。 在上图中,在插入之前将数组填充到索引4。 这样,您可以确定数组是否已满,以及应该在末尾插入元素时使用哪个索引。

After doing this, our element is successfully inserted.

完成此操作后,我们的元素已成功插入。

Wait️等一下! 如果阵列已满怎么办? (⚠️ Wait a minute! What Happens if the Array is Full?)

What do you think will happen if the array is full and you try to insert an element?

如果数组已满并且尝试插入元素,您会怎么办?

In this case, you need to create a new, larger array and manually copy all the elements into this new array. This operation is very expensive, time-wise. Imagine what would happen if you had an array with millions of elements! That could take a very long time to complete. ⏳

在这种情况下,您需要创建一个更大的新数组,并将所有元素手动复制到该新数组中。 在时间上,此操作非常昂贵。 想象一下,如果您拥有一个包含数百万个元素的数组,将会发生什么! 这可能需要很长时间才能完成。 ⏳

Note: The only exception to this rule, when insertion is very fast, is when you insert an element at the end of the array (at the index located to the right of the last element) and there is still space available. This is done in constant time O(1).

注意:当插入速度非常快时,此规则的唯一例外是在数组末尾 (位于最后一个元素右边的索引处)插入元素,并且仍然有可用空间。 这是在恒定时间O(1)中完成的。

2️⃣删除-再见,再见! (2️⃣ Deletion— Bye, Bye!)

Now let’s say that you want to delete an element from the array.

现在假设您要从数组中删除一个元素。

To maintain the efficiency of random access (being able to access the array through an index extremely fast) the elements must be stored in contiguous spaces of memory. You can’t just delete the element and leave that space empty.

为了保持随机访问的效率(能够通过索引快速访问数组),必须将元素存储在连续的内存空间中。 您不能只删除元素并将该空间留空。

You should move the elements that come after the element that you want to delete one index the left.

您应该将要删除一个索引的元素之后的元素向左移动。

And finally, you have this resulting array. As you can see, “b” has been successfully deleted.

最后,您将得到结果数组。 如您所见,“ b”已成功删除。

Note: Deletion is very efficient when you remove the last element. Since you need to create a variable to keep track of the last index that contains elements (in the diagram above, index 3), you can directly remove that element using the index.

注意:当您删除第l ASTê字元素d eletion是非常有效的。 由于您需要创建一个变量来跟踪包含元素的最后一个索引(在上图中,索引3),因此可以使用索引直接删除该元素。

3️⃣寻找元素 (3️⃣ Finding an Element)

You have three options to find an element in an array:

您可以通过三个选项在数组中查找元素:

  • If you know where it’s located, use the index.

    如果您知道它的位置 ,请使用索引。

  • If you don’t know where it’s located and your data is sorted, you can use algorithms to optimize your search, such as Binary Search.

    如果您不知道它的位置并且对数据进行了排序 ,则可以使用算法来优化搜索,例如二进制搜索。

  • If you don’t know where it’s located and your data is not sorted, you will need to search through every element in the array and check if the current element is the element you are looking for (please see the sequence of diagrams below).

    如果您不知道它的位置并且未对数据进行排序 ,则需要搜索数组中的每个元素,并检查当前元素是否是您要查找的元素(请参见下面的图序列)。

综上所述… (In Summary…)

  • Arrays are extremely powerful data structures that store elements of the same type. The type of elements and the size of the array are fixed and defined when you create it.

    数组是功能非常强大的数据结构 ,用于存储相同类型的元素。 创建数组时,元素的类型和数组的大小是固定的并已定义。

  • Memory is allocated immediately after the array is created and it’s empty until you assign the values.

    创建数组后立即分配内存,在分配值之前该内存为空。

  • Their elements are located in contiguous locations in memory, so they can be accessed very efficiently (random access, O(1) = constant time) using indices.

    它们的元素位于内存中的相邻位置 ,因此可以使用index高效地访问它们(随机访问,O(1)=恒定时间)。

  • Indices start at 0, not 1 like we are used to.

    索引从0开始 ,而不是我们习惯的1。

  • Inserting elements at the beginning or in the middle of the array involves moving elements to the right. If the array is full, creating a new, larger array (which is not very efficient). Inserting at the end of the array is very efficient, constant time O(1).

    在数组的开头或中间插入元素需要向右移动元素。 如果阵列已满,则创建一个更大的新阵列(效率不高)。 在数组末尾插入非常高效,恒定时间为O(1)。

  • Removing elements from the beginning or from the middle of the array involves moving all the elements to the left to avoid leaving an empty space in memory. This guarantees that the elements are stored in contiguous spaces in memory. Removing at the end of the array is very efficient because you only delete the last element.

    从数组的开头或中间删除元素涉及将所有元素向左移动,以避免在内存中留出空白空间。 这样可以确保将元素存储在内存中的连续空间中。 在数组末尾删除非常有效,因为您只删除最后一个元素。

  • To find an element, you need to check the entire array until you find it. If the data is sorted, you can use algorithms such as Binary Search to optimize the process.

    要找到一个元素 ,您需要检查整个数组直到找到它。 如果对数据进行了排序,则可以使用“二进制搜索”之类的算法来优化流程。

“Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.”

“从昨天开始学习,为今天而活,为明天充满希望。 重要的是不要停止质疑。”

— Albert Einstein

- 艾尔伯特爱因斯坦

谢谢! (Thank you!)

I really hope that you liked my article. ❤️Follow me on Twitter to find more articles like this one.

我真的希望你喜欢我的文章。 ❤️ Twitter上 关注我,以查找更多与此类似的文章。

翻译自: https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/

适合初学者的数据结构

适合初学者的数据结构_数据结构101:数组-初学者的直观介绍相关推荐

  1. 适合初学者的数据结构_数据结构101:图-初学者的直观介绍

    适合初学者的数据结构 了解您每天使用的数据结构 (Get to know the data structures that you use every day) Welcome! Let's Star ...

  2. java 堆和栈 数据结构_数据结构的栈和堆和程序中的堆和栈

    在计算机领域,堆栈是一个不容忽视的概念,我们编写的C语言程序基本上都要用到.但对于很多的初学着来说,堆栈是一个很模糊的概念.堆栈:一种数据结构.一个在程序运行时用于存放的地方,这可能是很多初学者的认识 ...

  3. java hashtable 数据结构_数据结构--哈希表(Java)

    数据结构--哈希表(Java) 介绍 哈希表 底层是 数组加链表 或者是 数组加二叉树 ,一个数组里面有多个链表,通过散列函数来提高效率 代码 package cn.guizimo.hashtab; ...

  4. vb6实现union数据结构_数据结构与算法——并查集(不相交集合)

    首发公众号:bigsai 认识并查集 对于并查集(不相交集合),很多人会感到很陌生,没听过或者不是特别了解.实际上并查集是一种挺高效的数据结构.实现简单,只是所有元素统一遵从一个规律所以让办事情的效率 ...

  5. 算法与数据结构_数据结构与算法专题--算法基本概念

    很多开发者都知道"程序=数据结构+算法"这个著名的公式,并不真正明白算法的定义或概念.究竟什么是算法呢?从字面意义上理解,算法即用于计算的方法,通过这种方法可以达到预期的计算结果. ...

  6. #中队列的数据结构_数据结构与算法拓展(一)

    栈与队列 申明:由于篇幅限制,文章可能有些简略,如果大家想要详细了解,请一定要百度一下,并阅读例题,完成习题 绪言:计算机科学在过去的数十年内发展飞速,各种新颖的技术纷至沓来:5G,VR,AI:大数据 ...

  7. c语言必背数据结构_数据结构(C语言)考试重点必背

    第一章: 绪论 1.1 :数据结构课程的任务是:讨论数据的各种 逻辑结构 .在计算机中的存储结构以及 各种操作的算法设计. 1.2 :数据:是客观描述事物的数字.字符以及所有的能输入到计算机中并能被计 ...

  8. java string 数据结构_数据结构---Java---String、StringBuilder、StringBuffer

    1.概述 1.1 String:不可变字符串 public final class String implements java.io.Serializable, Comparable, CharSe ...

  9. 目录树 删除 数据结构_数据结构:B树和B+树的插入、删除图文详解

    B树 1.1B树的定义 B树也称B-树,它是一颗多路平衡查找树.我们描述一颗B树时需要指定它的阶数,阶数表示了一个结点最多有多少个孩子结点,一般用字母m表示阶数.当m取2时,就是我们常见的二叉搜索树. ...

最新文章

  1. 高速上车辆追尾无法移动 众人齐力抬车温暖归途
  2. 各类web服务器 http access log获取用户和代理ip
  3. 不是你无法入门自然语言处理(NLP),而是你没找到正确的打开
  4. Uncaught TypeError: Object #Document has no method 'load'
  5. mysql 以 db 结尾_MySQL的高级部分
  6. 流程平台:示例流程 - 主机申请审批流程
  7. Javascript脚本之清除浏览器历史数据
  8. 数据结构课程设计之学生宿舍信息管理系统
  9. ROS系列:八、图像消息和OpenCV图像之间进行转换-cv_bridge
  10. dedecms如何制作中英文切换的网站?
  11. 盘点 6 个开源的音乐播放器!
  12. 华为交换机密码遗失怎么办?华为交换机密码恢复方法
  13. java计算机毕业设计雁门关风景区宣传网站源码+系统+mysql数据库+lw文档
  14. 启动cartographer_ros进行仿真错误process has died exit code -11
  15. BD新标签页-最值得安装的浏览器插件
  16. 嫁人就要嫁程序员,钱多话少死得早!
  17. 这应该是关于GPS定位写得最详实清晰的文章之一
  18. GCC和Clang的两个值得了解的编译器开关
  19. 梅特卡夫定律vs齐普夫定律vs邓巴“150定律”
  20. QML控件类型:Menu

热门文章

  1. iOS SwiftUI篇-3 排版布局layout
  2. Ant Design of React从入门到开发教程
  3. 【iOS】通讯录分组方式展示数据
  4. 参加第一届宇宙 JavaScript 大会是怎样的体验
  5. LNMP安装与启动脚本编写
  6. 实战:使用Telnet排除网络故障
  7. php代码中使用换行及(\n或\r\n和br)的应用
  8. 【斗医】【18】Web应用开发20天
  9. 深入掌握JMS(一):JMS基础
  10. centos安装easy_instal