当前位置: 首页 > 编程日记 > 正文

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

适合初学者的数据结构

了解您每天使用的数据结构。 (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/

适合初学者的数据结构

相关文章:

少侠,找个千手观音来帮你营销可好?

亚历山大公司营销主管老张最近有点儿烦&#xff0c;不是因为老婆更年期、女儿叛逆期&#xff0c;而是工作遇到了些麻烦。 社交营销很火&#xff0c;老张自认为公司始终游走在新科技最前沿&#xff0c;当然在第一时间就开通了微信、微博、QQ……各种社交网络的一大堆账号&#x…

Upload上传图片

实现antd上传图片,Upload 组件可以上传多张图片,多张图片上传成功的效果图: 每次上传 onChange 回调函数都会执行一次并且里面接收一个JSON对象,其中 file 对象是本次上传的图片信息,status 值为 done 就表示这一次上传成功了,fileList 中是一个数组,里面是组件所有上传…

将html中的代码拷贝到jsp后出现的问题 Failed to create the part's controls

Failed to create the parts controls 解决方法&#xff1a; 在文件上右键:open with转载于:https://www.cnblogs.com/flyoung/p/4885921.html

面试官问你想找什么工作_找工作时如何面试面试官

面试官问你想找什么工作在技​​术面试中要问的十二个问题 (Twelve questions to ask at tech interviews) I’ve just come off six weeks’ of interviewing for medior software developer roles, in a market that is desperate for talent (Amsterdam). That means I went…

windows7 端口查看以及杀死进程释放端口

1、调出命令窗口&#xff1a;开始---->运行---->cmd&#xff0c;或者是windowR组合键 2、输入命令&#xff1a;netstat -ano&#xff0c;列出所有端口的情况。在列表中我们观察被占用的端口&#xff0c;比如是4300&#xff0c;我们拿它来做实验。 3、查看被占用端口对应的…

web-view 跳转小程序页面 网页跳转小程序

H5实现代码&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>测试H5</title><meta content"widthdevice-width, initial-scale1.0, maximum-scale1.0, user-scalable0" name"viewport…

MongoDB安装指南

0. 环境说明&#xff1a;Ubuntu 14.04, MongoDB2.6.1 1.输入MongoDB中public Key值到Ubuntu包系统中 2. 在Sources列表中创建MongoDB的文件 3. 又一次载入本地的文件包库列表 4. 安装MongoDB数据库 5. 启动MongoDB 6. 启动MongoDB shell,shell提供了一个类似SQLConsole的方式…

待办事项优先级 开发_如何通过创建主题待办事项确定学习内容的优先级

待办事项优先级 开发by Dan Draper通过丹德雷珀(Dan Draper) 如何通过创建主题待办事项确定学习内容的优先级 (How to prioritize what you learn by creating a topic backlog) 25年编码经验 (Lessons from 25 years of coding) Way back in 1994, I started learning how to…

Luogu P1087 FBI树

P1087 FBI树 题目描述 我们可以把由“0”和“1”组成的字符串分为三类&#xff1a;全“0”串称为B串&#xff0c;全“1”串称为I串&#xff0c;既含“0”又含“1”的串则称为F串。 FBI树是一种二叉树&#xff0c;它的结点类型也包括F结点&#xff0c;B结点和I结点三种。由一个长…

小程序 url 对象转字符串编码传参 url 字符串转对象解码接收参数

url 对象转字符串编码传参 let info encodeURI(JSON.stringify(this.data.info));wx.navigateTo({url: /pages/partner_reward/recognition_result/result?info info,}) url 字符串转对象解码接收参数 onLoad(options){let info JSON.parse(decodeURI(options.info));},

入职体检体检错了_我们如何更新入职体验并获得更多用户

入职体检体检错了by William Woodhead威廉伍德黑德(William Woodhead) 我们如何更新入职体验并获得更多用户 (How we updated our onboarding experience and got more users) 我们过去将转化率提高60&#xff05;的方法 (Methods we used to increase conversion by 60%) As …

Java集合框架:EnumMap

EnumMap定义 package java.util;import java.util.Map.Entry; import sun.misc.SharedSecrets; public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V>implements java.io.Serializable, Cloneable{private final Class<K> keyType;p…

javascript eval和JSON之间的联系

eval函数的工作原理 eval函数会评估一个给定的含有JavaScript代码的字符串&#xff0c;并且试图去执行包含在字符串里的表达式或者一系列的合法的JavaScript语句。eval函数将把最后一个表达式或者语句所包含的值或引用作为返回值。 举例说明 eval评估JavaScript表达式var bar …

notification antd 弹窗使用示例

示例代码 import { notification } from antd;notification.error({description: 您的网络发生异常&#xff0c;无法连接服务器,message: 网络异常,});

python如何编写数据库_如何在几分钟内用Python编写一个简单的玩具数据库

python如何编写数据库MySQL, PostgreSQL, Oracle, Redis, and many more, you just name it — databases are a really important piece of technology in the progress of human civilization. Today we can see how valuable data are, and so keeping them safe and stable…

齐博cms 7.0 漏洞分析

** 0x01 原理分析 ** 还是很早之前爆出来的漏洞&#xff0c;现在拿出来学习一下&#xff0c;参考阿里巴巴&#xff1a;https://security.alibaba.com/... 漏洞发生在/inc/common.inc.php页面中。首先看这个函数&#xff1a; 首先使用ini_get来获取php.ini中变量register_global…

J2EE 中的服务器 tomcat6.0 配置

Tomcat6.0 配置 第一步&#xff1a;下载jdk和tomcat&#xff1a;JDK下载 Tomcat下载 最新的jdk为1.6.10&#xff0c;tomcat为6.0&#xff0c;建议jdk1.4以上&#xff0c;tomcat4.0以上 第二步&#xff1a;安装和配置你的jdk和tomcat&#xff1a;执行jdk和tomcat的安装程序…

【Ant Design Pro 四】react 点击事件传参

简单的绑定点击事件传参&#xff1a; 点击事件 function myClick(){console.log(点击)}return (<Button onClick{myClick}>点击</Button>) 点击事件传参 sendGoods(e){console.log(sendGoods,e)}render() {retrun(<Button type"primary" onClick{(e…

初创公司为什么要我_在一家大型初创公司担任副总裁之前,我希望知道什么

初创公司为什么要我by Assaf Elovic通过阿萨夫埃洛维奇 在一家大型初创公司担任副总裁之前&#xff0c;我希望知道什么 (What I wish I knew before becoming a VP at a large startup) When I started my position as VP of R&D at a growing startup, I thought my bigg…

微信小程序自定义轮播图滚动样式 自定义组件轮播图的实现

效果图&#xff1a; 实现代码&#xff1a; wxml <view class"card card_b"><swiper autoplay"{{true}}" interval"4000" duration"500" current"{{swiperCurrent}}" bindchange"swiperChange" class&qu…

好看的dialog,sweet Alert Dialog 导入Android Studio

系统自带的dialog实在是丑到无法忍受。所以找到了一款比較好的第三方dialog。 github 地址例如以下: https://github.com/pedant/sweet-alert-dialog 老规矩&#xff0c;还是先看效果图&#xff01; 以下来介绍导入Android studio的方法 首先将github上的项目clone到本地。然后…

linux系统中删除文件夹

rm -rf 文件夹的名称 rm-r 文件名称转载于:https://www.cnblogs.com/chucklu/p/4890523.html

unity开发入门_Unity游戏开发终极入门指南

unity开发入门Unity is a great tool for prototyping everything from games, to interactive visualisations. In this article, we run through all you need to know to get started using Unity.Unity是一个很好的工具&#xff0c;可用于制作从游戏到交互式可视化等所有内…

uvalive 3218 Find the Border

题意&#xff1a;一条封闭折线将平面分成了若干个区域&#xff0c;按顺序给出折线各点的坐标&#xff0c;要求输出封闭折线的轮廓。 题解&#xff1a;用类似卷包裹的算法&#xff0c;先确定一个一定会被选中的点(x坐标最小&#xff0c;y坐标最小)作为起点&#xff0c;然后把可…

[Mac] mac linux 多线程下载利器 axel

​> 之前做过一些文件下载的统计&#xff0c;发现谷歌浏览器chrome和火狐firefox, 一般都是单线程的下载文件&#xff0c;360浏览器却是多线程的下载。如今切换到了mac上&#xff0c;发现没有360哪个浏览器&#xff0c;就像找个在linux或者mac下能够多线程下载的工具。 linu…

antd 表单提交,文件和表单内容一起提交,表单校验

用很简单的源码实现包含下列 antd 表单相关知识: 1.表单必填校验,规则校验 2.Upload 上传图片,获取上传图片的状态,如上传成功,上传失败,上传进度条,删除上传的文件 3.获取 Input 组件用户输入的值,设置默认值 4.提交表单不刷新页面 5.把上传的图片显示在页面 页面…

代码注释//_您应该停止编写//的五个代码注释,并且//应该开始的一个注释

代码注释//提供来自您最喜欢和最受欢迎的开源项目的示例-React&#xff0c;Angular&#xff0c;PHP&#xff0c;Pandas等&#xff01; (With examples from your favorite and most popular open source projects — React, Angular, PHP, Pandas and more!) 代码质量与注释之间…

eclipse安装maven

maven 下载地址&#xff1a;http://maven.apache.org/download.cgi 1.maven环境配置 将下载的maven解压到某一盘下&#xff0c;进入E:\maven\apache-maven-3.3.9\conf目录&#xff0c;修改setting.xml文件 找到<localRepository>节点&#xff0c;配置本地仓库的地址&…

微信小程序 循迹功能制作

规划地图的路径&#xff0c;实时获取用户当前的定位&#xff0c;进行路线循迹导航功能的开发&#xff1a; 效果图&#xff1a; 实现代码&#xff1a; <map id"map" enable-satellite longitude"{{longitude1}}" latitude"{{latitude1}}" sca…

DOM解析和SAX解析的区别

DOM解析和SAX解析的区别 博客分类&#xff1a; XMLDOM SAX DOM解析和SAX解析的区别 No区 别DOM解析SAX解析1操作将所有文件读取到内存中形成DOM树&#xff0c;如果文件量过大&#xff0c;则无法使用顺序读入所需要的文件内容&#xff0c;不会一次性全部读取&#xff0c;不受文件…