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

news/2024/7/3 0:43:38

适合初学者的数据结构

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

适合初学者的数据结构


http://lihuaxi.xjx100.cn/news/238521.html

相关文章

Java排序算法:冒泡排序

Java排序算法&#xff1a;冒泡排序//创建数组并赋值int[] data new int[] {11,10,55,78,100,111,45,56,79,90,345,1000};for(int i0;i < arr.length-1;i){for(int j 0; j < arr.length-i-1;j){if(arr[j] > arr[j1]){int temp arr[j];arr[j] arr[j1];arr[j1] temp;…

Kali Linux攻防系统(一:攻防系统Kali Linux下载安装与更新)

任务一&#xff1a;攻防系统Kali Linux下载安装与更新 1.1、安装Kali Linux虚拟机 1.1.1、电脑硬件配置至少达到 CPU 内存 存储 >四核 >4G >20G 1.1.2、VMware Workstations版本为14及以上&#xff1b; 1.1.3、虚拟机系统版本选择Debian 8.X或者Ubuntu&#x…

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

面试官问你想找什么工作在技​​术面试中要问的十二个问题 (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…

C语言技术讲解

想知道更多关于区块链技术知识&#xff0c;请百度【链客区块链技术问答社区】 链客&#xff0c;有问必答&#xff01;&#xff01;C语言&#xff1a; C语言是介于汇编语言和高级语言之间的语言&#xff0c;属于高级语言&#xff0c;也称为中级语言&#xff0c;是集汇编和高级语…

JDBC操作MySQL Lob字段记实

JDBC操作MySQL Lob字段记实虽然Java的持久化框架多如牛毛&#xff0c;但都离不开JDBC技术&#xff0c;JDBC在某些时候是其他框架难以取代的。也是java操作数据库最根本的技术。上文写了JDBC操作DB2 Lob字段bug问题&#xff0c;为此&#xff0c;我还特意写了MySQL平台下的Lob字段…

Kali Linux攻防系统(三:在Kali Linux系统中配置安全测试浏览器及系统清理备份)

任务三&#xff1a;配置安全测试浏览器及系统清理备份 3.1、汉化Firefox并安装安全插件 3.1.1、汉化Firefox浏览器&#xff0c;安装中文插件&#xff0c;并更改设置&#xff1b; 3.1.2、在浏览器附加组件管理器中查找“Web Developr”插件 3.1.3、安装添加附件组件 3.2、手动…

程序员真的会穷途末路吗?牵引力教育资深大佬这样说

随着互联网IT行业不断的迅速发展&#xff0c;软件开发行业中&#xff0c;往往透露这很多疑问&#xff0c;年龄歧视、职业的转向等方面。牵引力教育资深大佬带你了解职业规划会有哪些方向&#xff1f; 一、技术方向 架构师&#xff0c;系统分析师,是CTO这种往往是走纯技术路线, …

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

待办事项优先级 开发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…