您现在的位置是:貌合心离网 > 热点
数组: 一种非常基础且重要的数据结构
貌合心离网2026-11-14【热点】{固定数字1}人已围观
简介谷歌霸屏技术【tg:seogoogle987】
前言
数组是一种非常基础且重要的数据结构,很多复杂的数据结构都是基于数组实现的。深入理解数据的存储原理和特点,有利于我们在实际开发工作中,充分发挥数据的优势。
数据是什么
数组的定义:数组(Array)是一种线性表数据结构。它用一组连续的内存空间,存储一组具有相同类型的数据。
在上面的定义中加黑的描述,我们可以发现数组的几个特点,分别是:线性表、连续的内存空间、相同类型的数据。如下图所示:
数组因具有连续的内存空间的特点,让数据拥有非常高效率的“随机访问”,但也是因为要保持这个连续的内存空间,导致数组在删除或插入操作的时非常低效。因为数组为了保持连续性,必然会涉及大量数据的搬移,这个是非常消耗时间数组的。
思考:这里你可能会有疑问:什么是连续的内存空间?
首先,我们来说说内存,内存是由一个个连续的内存单元组成的,每一个内存单元都有自己的地址。在这些内存单元中,有些被其他数据占用了,有些是空闲的。
然而数据中的每个元素,都存储在小小的内存单元中,并且元素之间紧密排列,既不能打乱元素的存储顺序,也不能跳过某个存储单元进行存储。
数组的随机访问
数组的随机访问是有个寻址公式的,上问中我们提到过数组是用一组连续的内存空间存储数据元素的,然而每个内存单元都有自己的地址(在计算机里面就是通过这个地址访问数据的),又加上每个内存单元的大小都是一样的,这样就很容易得到一个公式了,如下所示:
a[i]_address=bas: e_address+i*data_type_size
我们来简单解释一下上述公式,其中data_type_size表示数组中每个元素的大小、base_address表示内存块的首地址、i 表示数组下标。
数组的基本操作
在开始之前我们先创建一个数组类,来模拟数组操作时候的相关操作。代码如下:
public class MyArray { private int[] array; // 数组大小 一种; private int size; public MyArray(int capacity) { this.size = 0; this.ar非常ray = new int[capacity]; } } 1. 读取元素
我们知道数组在内存中是连续存储的,所以根据上文的寻址公式可以知道,我们可以根据数组下标 i 快速定位到对应的元素。
简单举例,代码如下:
int[] array={1,2,3,4,5,6}; System.out.println(array[1]); // 输出的是2 因为数组的下标是从0开始的。&nbs基础p; 2. 更新元素
我们可以根据数组下标快速查找到对应元素。那么同样道理,我们可以根据数组下标 i 快速更新元素,这中间涉及两个过程,首先就是找到数组下标 i 对应的数据元素A,然后将新的数据元素B赋值给A即完成更新。
简单举例,代码如下:
int[] array={1,2,3,4,5,6}; System.out.println(array[1]); // 输出的是2 //更新数组下标为 1 的数组元素 array[1]=22; System.out.println(array[1]);&nbs且重p;// 输出的是22 3. 插入元素
相比读取、更新操作,插入元素稍微复杂一些,分为以下两种情况:
尾部插入:首先,我们看看尾部插入,这种情况很简单,在数组的最后新增一个新的元素,此时对于原数组来说没有任何影响,时间复杂度为0(1)。如下图所示:
中间插入:如果在数组的中间位置插入元素的话,此时会对插入元素位置之后的元素产生影响,也就是这些数据需要向后依次挪动一个位置。如下图所示:
中间插入的代码如下:
/** * 插入元素 * @param index 待插入的位置&要的nbsp; * @param element 待插入的元素 */ public void insert(int index,int element){ if(indexsize){ throw new IndexOutOfBoundsException("超过数组容量 ! 插入失败!"); &数据nbsp; } // 从左到右,将元素向右移动一位 for (int i=size-1 ; i>index ; i--){ array[i+1]=array[i]; }  结构; // 此时index这个位置已经腾空了,可以放进入element array[index]=element; //数组中元素个数+1 size++; }
3.1 数组扩容
因为数组的长度在创建的时候已经确定了,当插入元素的时候如果数组已经满了,是没办法插入成功的。这个时候就要考虑数组扩容的问题了,那么该如何实现扩容呢?
其实我们可以这样,比如此时的数组是A, A已经满了,我们再创建一个数组B且数组长数组度是A的2倍,然后我们将数组A的元素全部放到数组B中,这样就完成了数组扩容了。
数组扩容的代码如下:
/** * 数组扩容为原数组的二倍 */ public void resize(){ int[] newArray=new int[array.length*2]; System.arraycopy(array,0,newArray,0,array.length); array=newArray; 4. 删除元素
删除元: 素和插入元素类似,如果我们删除第k个位置的数据,为了内存的连续性,同样会涉及数据的挪动。如下图所示:
删除元素的代码如下:
/** * 根据数组下标删除元素 * * @param index 数组下标 * @return */ public int delete(int index) { if (index <&n一种bsp;0 || index > size) { throw new IndexOutOfBoundsException("已经超过数组容量 ! 插入失败!"); } int deleteElement = array[index]; &nb非常sp; // 从左到右,将元素向左移动一位 for (int i = index; i < size - 1; i++) { array[i] = array[i + 1]; }&基础nbsp; size--; return deleteElement; } 总结
数组是使用一块连续的内存空间,存储相同类型的一组数据,其最大的优点是数组支持随机访问,是因为数组可以通过数组下标(寻址公式)快速访问对应元素,时间复杂度为O(1)。
数组在删除元素和插入元素这两个操作比较低效,是因为数组为了保持数据的连续性,会涉及到数据的挪动,平均时间复杂度为O(N)。
很赞哦!(529)
站长推荐
友情链接
- 标签: 康师傅
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌霸屏技术【tg:seogoogle987】3hI
- https://www.nextavenue.org/?action=QUERY&s=谷歌霸屏技术【tg:seogoogle987】,8wQ
- https://www.qylaws.com/search/谷歌霸屏技术【tg:seogoogle987】,kjW
- https://www.sbaad.no/?s=谷歌霸屏技术【tg:seogoogle987】,I28
- https://www.xbiao.com/search/index?tpf&wd=谷歌霸屏技术【tg:seogoogle987】,fj8
- https://www.swecobelgium.be/?page=9&s=谷歌霸屏技术【tg:seogoogle987】,3ce
- https://www.nbm.org/?action=QUERY&s=谷歌霸屏技术【tg:seogoogle987】,EDa
- https://www.usslexington.com/?action=QUERY&s=谷歌霸屏技术【tg:seogoogle987】,40x
- https://www.paspaley.com/catalogsearch/result/?action=QUERY&q=谷歌霸屏技术【tg:seogoogle987】,Liy
- https://www.woorden.org/spreekwoord.php?woord=谷歌霸屏技术【tg:seogoogle987】,t4r
- https://www.jusoen.com/addreng.asp?p1=谷歌霸屏技术【tg:seogoogle987】hmF
- https://www.scq.ubc.ca/?page=2&s=谷歌霸屏技术【tg:seogoogle987】,pTU
- https://www.thedp.com/page/search?q=谷歌霸屏技术【tg:seogoogle987】,xXV
- https://www.losgatoschamber.com/list/search?action=SEARCH&q=谷歌霸屏技术【tg:seogoogle987】,ryZ
- https://www.hanbat.ac.kr/search/front/Search.jsp?qt=谷歌霸屏技术【tg:seogoogle987】ygl
- https://www.mesec.cz/vyhledavani/?qs=谷歌霸屏技术【tg:seogoogle987】,yuX
- https://www.tarleton.edu/?s=谷歌霸屏技术【tg:seogoogle987】,m33
- https://www.realsporting.com/buscador?buscar=谷歌霸屏技术【tg:seogoogle987】,aix
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌霸屏技术【tg:seogoogle987】34a
- https://www2.ual.es/ProyectoLates/?s=谷歌霸屏技术【tg:seogoogle987】,Zd0
- https://www.remit.co.jp/vi/?s=谷歌霸屏技术【tg:seogoogle987】,bZe
- https://www.maafushidive.com?collections?vendors?q=谷歌霸屏技术【tg:seogoogle987】,8Mb
- https://www.yr.no/en/search?q=谷歌霸屏技术【tg:seogoogle987】,yNA
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏技术【tg:seogoogle987】w12
- https://www.schenckprocess.com/search?q=谷歌霸屏技术【tg:seogoogle987】,C7u
- https://www.teamlab.art/zh-hans/?submit=谷歌霸屏技术【tg:seogoogle987】,B4p
- https://www.ussalabama.com/?action=CHECK&s=谷歌霸屏技术【tg:seogoogle987】,O6z
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌霸屏技术【tg:seogoogle987】npO
- https://www.nyif.com/catalogsearch/result/?q=谷歌霸屏技术【tg:seogoogle987】,Wle
- https://www.impactpool.org/search/?type=79&q=谷歌霸屏技术【tg:seogoogle987】qb5
- https://www.kissthekangaroo.com.au/index.php?route=product/search&tag=谷歌霸屏技术【tg:seogoogle987】,AHk
- https://www.oprny.com/search.php?search_query=谷歌霸屏技术【tg:seogoogle987】,W5Y
- https://www.jusoen.com/addreng.asp?p1=谷歌霸屏技术【tg:seogoogle987】v7C
- https://www.remit.co.jp/zh/?s=谷歌霸屏技术【tg:seogoogle987】,nkF
- https://www.skicheck.at/en/?action=CHECK&s=谷歌霸屏技术【tg:seogoogle987】,gYC
- https://www.miamiopenmri.com/?action=CHECK&s=谷歌霸屏技术【tg:seogoogle987】,4mE
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌霸屏技术【tg:seogoogle987】QoR
- https://www.smics.com/tc/site/search?token=谷歌霸屏技术【tg:seogoogle987】,4f6
- https://www.southmeadows.co.uk/2019/07/?s=谷歌霸屏技术【tg:seogoogle987】,HtS
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=谷歌霸屏技术【tg:seogoogle987】,gS7
- https://m.shoppinghow.kakao.com/m/search/q/谷歌霸屏技术【tg:seogoogle987】NDG
- https://www.vet-direct.com/search?cat=72&term=谷歌霸屏技术【tg:seogoogle987】,qdA
- https://www.dba.dk/soeg/?soeg=谷歌霸屏技术【tg:seogoogle987】Mz3
- https://www.riohondo.edu/?action=QUERY&s=谷歌霸屏技术【tg:seogoogle987】,wNk
- https://www.sweat.com/collections/vendors?q=谷歌霸屏技术【tg:seogoogle987】,qfD
- https://wydden.com/?ID=r8w9WMSKWkOcmiaFWaUv0dzMdqZWfKtF&s=谷歌霸屏技术【tg:seogoogle987】,tHT
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌霸屏技术【tg:seogoogle987】,JKF
- https://zhidao.baidu.com/index/?action=QUERY&word=谷歌霸屏技术【tg:seogoogle987】,NGz
- https://www.mynextmove.org/find/search?s=谷歌霸屏技术【tg:seogoogle987】,ojQ
- https://www.teamlab.art/fr/?submit=谷歌霸屏技术【tg:seogoogle987】,FYE







