admin 管理员组文章数量: 1184232
目录
python下标越界list index out of range
Java下标越界java.lang.ArrayIndexOutOfBoundsException
Java String index out of range
C语言数组下标越界out terminated
C++数组下标越界不报错
C#数组下标越界System.IndexOutOfRangeException
PHP数组下标越界Notice: Undefined offset:1
总结
python下标越界list index out of range
python里说的是list越界,其实也就是列表,格式是【arr=["1","2","3"]】这样的,如果输出的时候超出下标数量就会报这个异常。来我们截图看一下。
还是很明显的。
Java下标越界java.lang.ArrayIndexOutOfBoundsException
无论是数组还是集合列表在下标越界的时候都会报这个异常,只是后续提示的内容有所区别,我们来看一下:
String [] arr=new String[10];
System.out.println(arr[11]);
ArrayList<Integer> arr=new ArrayList<Integer>();
System.out.println(arr.get(11));
Java String index out of range
Java字符串超出索引的范围,这里的out of是【越出…之外】的意思,range就是【范围】,超出范围肯定会报错的。
C语言数组下标越界out terminated
#include <stdio.h>
int main() {
int a[10]={0};
a[10] = 0;
return 0;
}
C语言的下标越界报错有些不同。
*** stack smashing detected ***: ./970872.out terminated
可以看到包的错不是index out of,是out terminated,也就是超出终止范围。
画一个小爱心,祝大家都能顺利解决各种bug。
#include <stdio.h>
int main() {
printf("\n\n绘制一个心形图案:");
for (float y = 1.5f; y > -1.5f; y -= 0.1f) {
for (float x = -1.5f; x < 1.5f; x += 0.05f) {
float a = x * x + y * y - 1;
putchar(a * a * a - x * x * y * y * y <= 0.0f ? '*' : ' ');
}
putchar('\n');
}
return 0;
}
C++数组下标越界不报错
C++比较特殊,下标越界的时候啥也不报,这个挺难受的。
#include <stdio.h>
#include <iostream>
#include <math.h>
int main() {
using namespace std;
char a[]={'1','2'};
cout << a[5]<<endl;
return 0;
}
很明显的上列代码是有越界的,执行的时候啥都没有,不报错。
linux平台或者g++编译器对下标越界问题有自己的处理,导致程序不挂。而Visual c++编译器对下标越界不进行处理,程序挂掉。
来个好看一些的心来平绪一下心情吧:
#include <stdio.h>
#include <iostream>
#include <math.h>
float f(float x, float y, float z) {
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}
float h(float x, float z) {
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if (f(x, y, z) <= 0.0f) {
return y;
}
return 0.0f;
}
int main() {
using namespace std;
for (float z = 1.5f; z > -1.5f; z -= 0.05f) {
for (float x = -1.5f; x < 1.5f; x += 0.025f) {
float v = f(x, 0.0f, z);
if (v <= 0.0f) {
float y0 = h(x, z);
float ny = 0.01f;
float nx = h(x + ny, z) - y0;
float nz = h(x, z + ny) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
putchar(".:-=+*#%@"[(int)(d * 5.0f)]);
} else {
putchar(' ');
}
}
putchar('\n');
}
return 0;
}
C#数组下标越界System.IndexOutOfRangeException
C#的越界和Java的差不多,都会提示出来,直接说明是怎么回事。有中文提示还是挺方便的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo20221008
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[5];
Console.WriteLine(arr[6]);
}
}
}
PHP数组下标越界Notice: Undefined offset:1
Notice: Undefined offset: 22 in D:\phpStudy\PHPTutorial\WWW\demo.php on line 4
这是PHP的数组下标越界,一看就明白,未定义的开端,也就是没有这个开始。那肯定就是越界了。
总结
其它小众的语言我就不举例了,我们挨个看了看常用的语言数组下标越界的情况,从而能在以后的异常中快速定位问题所在快速解决bug。
异常是很常见的,一个不会解决bug的程序员是很难找到成就感的,当你突破一个个bug后那种自豪感还是很棒的。
本文标签: 下标 几种 解决方案 IndexOutOf
版权声明:本文标题:IndexOutOf下标越界几种解决方案 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1766203064a3444227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论