您现在的位置是:热点 >>正文
数组: 一种非常基础且重要的数据结构
热点527692人已围观
简介...
前言
数组是一种非常基础且重要的数据结构,很多复杂的数据结构都是基于数组实现的。深入理解数据的存储原理和特点,有利于我们在实际开发工作中,充分发挥数据的优势。
数据是什么
数组的定义:数组(Array)是一种线性表数据结构。它用一组连续的内存空间,存储一组具有相同类型的数据。
在上面的定义中加黑的描述,我们可以发现数组的几个特点,分别是:线性表、连续的内存空间、相同类型的数据。如下图所示:
数组因具有连续的内存空间的特点,让数据拥有非常高效率的“随机访问”,但也是因为要保持这个连续的内存空间,导致数组在删除或插入操作的时非常低效。因为数组为了保持连续性,必然会涉及大量数据的搬移,这个是非常消耗时间数组的。
思考:这里你可能会有疑问:什么是连续的内存空间?
首先,我们来说说内存,内存是由一个个连续的内存单元组成的,每一个内存单元都有自己的地址。在这些内存单元中,有些被其他数据占用了,有些是空闲的。
然而数据中的每个元素,都存储在小小的内存单元中,并且元素之间紧密排列,既不能打乱元素的存储顺序,也不能跳过某个存储单元进行存储。
数组的随机访问
数组的随机访问是有个寻址公式的,上问中我们提到过数组是用一组连续的内存空间存储数据元素的,然而每个内存单元都有自己的地址(在计算机里面就是通过这个地址访问数据的),又加上每个内存单元的大小都是一样的,这样就很容易得到一个公式了,如下所示:
a[i]_address=base_: address+i*data_type_size
我们来简单解释一下上述公式,其中data_type_size表示数组中每个元素的大小、base_address表示内存块的首地址、i 表示数组下标。
数组的基本操作
在开始之前我们先创建一个数组类,来模拟数组操作时候的相关操作。代码如下:
public class MyArray { private int[] array; // 数组大小 &n一种bsp; private int size; public MyArray(int capacity) { this.size = 0; this.array非常 = new int[capacity]; } } 1. 读取元素
我们知道数组在内存中是连续存储的,所以根据上文的寻址公式可以知道,我们可以根据数组下标 i 快速定位到对应的元素。
简单举例,代码如下:
int[] array={1,2,3,4,5,6}; System.out.println(array[1]); // 输出的是2 因为数组的下标是从0开始的。 基础 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]); /且重/ 输出的是22 3. 插入元素
相比读取、更新操作,插入元素稍微复杂一些,分为以下两种情况:
尾部插入:首先,我们看看尾部插入,这种情况很简单,在数组的最后新增一个新的元素,此时对于原数组来说没有任何影响,时间复杂度为0(1)。如下图所示:
中间插入:如果在数组的中间位置插入元素的话,此时会对插入元素位置之后的元素产生影响,也就是这些数据需要向后依次挪动一个位置。如下图所示:
中间插入的代码如下:
/** * 插入元素 * @param index 待插入的位置&nb要的sp; * @param element 待插入的元素 */ public void insert(int index,int element){ if(indexsize){ throw new IndexOutOfBoundsException("超过数组容量 ! 插入失败!"); &nb数据sp; } // 从左到右,将元素向右移动一位 for (int i=size-1 ; i>index ; i--){ array[i+1]=array[i]; } &nbs结构p; // 此时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]; 非常 // 从左到右,将元素向左移动一位 for (int i = index; i < size - 1; i++) { array[i] = array[i + 1]; }&nbs基础p; size--; return deleteElement; } 总结
数组是使用一块连续的内存空间,存储相同类型的一组数据,其最大的优点是数组支持随机访问,是因为数组可以通过数组下标(寻址公式)快速访问对应元素,时间复杂度为O(1)。
数组在删除元素和插入元素这两个操作比较低效,是因为数组为了保持数据的连续性,会涉及到数据的挪动,平均时间复杂度为O(N)。
Tags:
相关文章
标签: 衡封新材 光刻胶树脂 酚醛树脂 电子级环氧树脂
热点云梦级1月27日云梦级消息,上海衡封新材料科技有限公司(以下简称“衡封新材”)于近日完成近亿元B轮融资, 本轮融资由华舆转型升级基金(中车资本旗下)、涌铧投资、临港数科基金、大零号湾策源基金和上海元禾...
【热点】
阅读更多标签: 智己LS8 智己汽车 奢华智选包 9800元选装包
热点云梦级6月18日云梦级消息,据新浪科技报道,今年 4 月刚刚上市的智己 LS8,首批车主在购车时花费 9800 元的选装包在仅一个多月后就变成了免费赠送。他们向智己汽车发布了两封联名信,要求首批车主获...
【热点】
阅读更多标签: 苹果 App Store 网页版 iOS
热点云梦级11月4日云梦级消息,有媒体报道,苹果或将于近期正式推出功能完整的网页版 App Store。另据新浪科技,用户无需持有 iPhone 等苹果设备,即可通过浏览器访问 apps.apple.co...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.sic-info.org/?s=谷歌霸屏外推技术【tg:seogoogle987】,rNU
- https://www.kingston.gov.uk/site-search/results/?q=谷歌霸屏外推技术【tg:seogoogle987】,O4X
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌霸屏外推技术【tg:seogoogle987】,Rr5
- https://www.qylaws.com/search/谷歌霸屏外推技术【tg:seogoogle987】,OuC
- https://www.rea.govt.nz/search?searchterm=谷歌霸屏外推技术【tg:seogoogle987】,NHF
- https://www.abbreviations.com/谷歌霸屏外推技术【tg:seogoogle987】Enc
- https://www.zalora.sg/catalog/?cat=112&q=谷歌霸屏外推技术【tg:seogoogle987】,Jep
- https://www.metaltix.com/?view=tours&filter=search&q=谷歌霸屏外推技术【tg:seogoogle987】,LYZ
- https://www.wilsonart.com/catalogsearch/result/?action=CHECK&q=谷歌霸屏外推技术【tg:seogoogle987】,o7A
- https://www.lindseymedicalsupply.com/search/谷歌霸屏外推技术【tg:seogoogle987】,yUL
- https://www.swecobelgium.be/?page=9&s=谷歌霸屏外推技术【tg:seogoogle987】,kYW
- https://www.skicheck.at/en/?action=CHECK&s=谷歌霸屏外推技术【tg:seogoogle987】,wet
- https://www.whiterock2008.com.tw/search?q=谷歌霸屏外推技术【tg:seogoogle987】,KCs
- https://www.tailoy.com.pe/catalogsearch/result/?q=谷歌霸屏外推技术【tg:seogoogle987】,HX6
- https://www.teamlab.art/es/?submit=谷歌霸屏外推技术【tg:seogoogle987】,vLE
- https://www.madebyoranges.com/nl/search/谷歌霸屏外推技术【tg:seogoogle987】,cvo
- https://www.springfieldlibrary.org/?s=谷歌霸屏外推技术【tg:seogoogle987】,4DT
- https://www.theaerogroup.net/?cat=28&s=谷歌霸屏外推技术【tg:seogoogle987】,NOJ
- https://www.oprny.com/search.php?search_query=谷歌霸屏外推技术【tg:seogoogle987】,e0x
- https://www.manga-news.com/index.php/recherche/?q=谷歌霸屏外推技术【tg:seogoogle987】,Lts
- https://www.lssystems.co.uk/search?page=0&per_page=24&term=谷歌霸屏外推技术【tg:seogoogle987】,JQF
- https://www.nss.nhs.scot/search?q=谷歌霸屏外推技术【tg:seogoogle987】,WBE
- https://dict.naver.com/dict.search?dicQuery&query=谷歌霸屏外推技术【tg:seogoogle987】4To
- https://www.pierreherme.com/catalogsearch/result/?q=谷歌霸屏外推技术【tg:seogoogle987】,jC0
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏外推技术【tg:seogoogle987】Czp
- https://www.samen-maier.at/search/谷歌霸屏外推技术【tg:seogoogle987】,Itf
- https://www.kores.com/?s=谷歌霸屏外推技术【tg:seogoogle987】,H7E
- https://www.selfsufficientish.com/main/?s=谷歌霸屏外推技术【tg:seogoogle987】,NLm
- https://www.miamiopenmri.com/?action=CHECK&s=谷歌霸屏外推技术【tg:seogoogle987】,HOl
- https://www.nextavenue.org/?action=QUERY&s=谷歌霸屏外推技术【tg:seogoogle987】,G0o
- https://www.mingwai.edu.hk/?langzh-hant&s=谷歌霸屏外推技术【tg:seogoogle987】,1Z1
- https://www.silentiumpc.com/en/search/谷歌霸屏外推技术【tg:seogoogle987】,mAB
- https://www.scq.ubc.ca/?page=2&s=谷歌霸屏外推技术【tg:seogoogle987】,jVf
- https://www.spoiledsweetboutique.com/search/谷歌霸屏外推技术【tg:seogoogle987】,eY6
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌霸屏外推技术【tg:seogoogle987】CTu
- https://www.wikiart.org/pt/Search/谷歌霸屏外推技术【tg:seogoogle987】,hYH
- https://www.solerpalau.com/en-en/catalogsearch/result/?q=谷歌霸屏外推技术【tg:seogoogle987】,jzO
- https://www.resopal.de/catalogsearch/result/?action=SEARCH&q=谷歌霸屏外推技术【tg:seogoogle987】,ALx
- https://zhidao.baidu.com/index/?action=QUERY&word=谷歌霸屏外推技术【tg:seogoogle987】,WSO
- https://www.repetto.jp/item/search?action=QUERY&q=谷歌霸屏外推技术【tg:seogoogle987】,2cd
- https://www.world-architects.com/en/search?term=谷歌霸屏外推技术【tg:seogoogle987】,6XN
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌霸屏外推技术【tg:seogoogle987】zbB
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌霸屏外推技术【tg:seogoogle987】hlg
- https://www.ussalabama.com/?action=CHECK&s=谷歌霸屏外推技术【tg:seogoogle987】,CfL
- https://www.remit.co.jp/vi/?s=谷歌霸屏外推技术【tg:seogoogle987】2xI
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌霸屏外推技术【tg:seogoogle987】,R8p
- https://www.sweat.com/collections/vendors?q=谷歌霸屏外推技术【tg:seogoogle987】,DMV
- https://www.maafushidive.com?collections?vendors?q=谷歌霸屏外推技术【tg:seogoogle987】,8f2
- https://www.sic-info.org/en/?s=谷歌霸屏外推技术【tg:seogoogle987】,45T
- https://www.nyif.com/catalogsearch/result/index/?f=sarbanesoxleyU&index=me_cms_pe&lit=20&mode=list&p=1&q=谷歌霸屏外推技术【tg:seogoogle987】,IvD