for (int i = this.usedSize-1; i >= pos ; i--) { this.elem[i+1] = this.elem[i]; } //2.插入数据 this.elem[pos] = data; //3. usedSize++ this.usedSize+ 为什么此处eleme没越界
时间: 2024-04-06 16:28:46 浏览: 10
根据代码片段中的循环语句,可以看出在插入数据之前,数组元素从pos到this.usedSize-1已经向右移动了一位,也就是说,pos位置原来的元素已经被移动到了pos+1的位置,因此在执行this.elem[pos] = data时,pos位置上原来的元素已经被正确地移动到了pos+1的位置,没有越界。同时,由于在插入数据之前,数组中已经有this.usedSize个元素,因此在插入数据之后,数组的元素数量将会增加1,不会越界。
相关问题
for (int i = this.usedSize-1; i >= pos ; i--) { this.elem[i+1] = this.elem[i]; }this.elem[pos] = data; this.usedSize++;那当i=this.usedSizw-1时,i+1没有越界吗,为啥
当 i = this.usedSize-1 时,i+1 确实会越界,因为数组的下标是从 0 开始的,所以数组的最后一个元素的下标是 this.usedSize-1。如果在循环中访问了 this.elem[i+1],则会导致数组越界异常。
但是,在这段代码中,循环体执行完毕后,程序会执行 this.elem[pos] = data; 这行代码,将 data 插入到 pos 位置上,这时候 pos 的值是 this.usedSize,因此 this.elem[pos] 实际上是访问到了顺序表的一个空闲位置,不会导致越界访问。同时,这里还有一行代码 this.usedSize++; 用于将顺序表的长度加 1,表示插入了一个新的元素。
因此,在这段代码中,虽然 i+1 确实会越界,但是这个越界访问并不会导致程序出错,因为这个位置是顺序表的一个空闲位置,能够存储新的元素。
在顺序线性表插入程序的编写中for(j=L.length-1;j>=i-1;j--)\nL.elem[j+1]=L.elem[j];//i之后元素后移\nL.elem(i-1)=e;//插入e\n++L.length;\nreturn Ok;的解析
这段代码是在顺序线性表中插入一个元素的程序。具体解析如下:
1. for(j=L.length-1;j>=i-1;j--):这个for循环是用来将插入位置之后的元素都向后移动一个位置,为插入新元素腾出空间。j的初始值为线性表的长度减1,即最后一个元素的下标。循环条件是j大于等于插入位置i-1,也就是从插入位置之后的元素开始逐个后移。每次循环j递减1。
2. L.elem[j+1]=L.elem[j]:这行代码是将当前位置j处的元素向后移动一个位置,即将元素j的值赋给位置j+1处。
3. L.elem(i-1)=e:这行代码是将新元素e插入到线性表的第i个位置,也就是将e赋值给L.elem(i-1)。
4. L.length:最后这行代码是将线性表的长度加1,表示插入了一个新元素。
5. return Ok:这个程序的返回值是Ok,表示插入操作成功执行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> <style> .content { padding: 15px; } i { margin-right: 10px; } .site-doc-icon li { width: 150px; height: 150px; line-height: 150px; margin: 0 auto 10px; text-align: center; background-color: #0099FF; cursor: pointer; color: #fff; border-radius: 50%; } .site-doc-icon li { display: inline-block; margin: 10px; } .site-doc-icon li { display: inline-block; } </style> </head> <body class="layui-layout-body"> <#include "head.ftl"> 首页 用户管理 分类管理 书评管理 统计占比 欢迎来到共享图书系统
<#include "bottom.ftl"> </body> <script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script> <script type="text/javascript" src="/js/axquery.js"></script> <script type="text/javascript" src="/js/template-web.js"></script> <script type="text/javascript" src="/layui/layui.all.js"></script> <script> layui.use([], function () { var $ = layui.jquery; //演示动画开始 $('.site-doc-icon .layui-anim').on('click', function () { var othis = $(this), anim = othis.data('anim'); //停止循环 if (othis.hasClass('layui-anim-loop')) { return othis.removeClass(anim); } othis.removeClass(anim); setTimeout(function () { othis.addClass(anim); }); //恢复渐隐 if (anim === 'layui-anim-fadeout') { setTimeout(function () { othis.removeClass(anim); }, 1300); } }); //演示动画结束 }) </script> </html>如何和后端交互的
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)