Java异常理解之Exception in thread “main“ java.lang.ArrayIndexOutOfBoundsException
这个异常是Java中的数组越界问题
当你使用不合法的索引访问数组是会出现这种错误
例如:
class Solution {
public static int climbStairs(int n) {
if (n == 1 || n == 2) {
return n;
}
int[] demo = new int[n];
demo[1]=1;
demo[2]=2;
for (int i = 3; i <=n; i++) {
demo[i] = demo[i-1] + demo[i-2];
}
return demo[n];
}
}
public class palouti {
public static void main(String[] args) {
System.out.println(Solution.climbStairs(3));
}
}
发生这种错误的原因是:
在这个地方开辟数组长度为n
int[] demo = new int[n];
而在下面的使用中
for (int i = 3; i <=n; i++) {
demo[i] = demo[i-1] + demo[i-2];
}
无论n的值为多少都会超出界限
因为数组的索引是从 0 开始的,前面开辟的长度每次都差了一位
例如:n=3时,在 for 循环的索引中,都会索引到 demo[3],而从 0 开始索引,到demo[3]时,就相当于从 0-1-2-3,超出了数组界限
解决方法也很简单
只要将
int[] demo = new int[n];
改为
int[] demo = new int[n+1];
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:
这个异常会经常遇到,只要注意数组的界限,就可以避免了
Java异常理解之Exception in thread “main“ java.lang.ArrayIndexOutOfBoundsException的更多相关文章
- java 错误之:Exception in thread "main" java.lang.NoClassDefFoundError
Exception in thread "main" java.lang.NoClassDefFoundError: PointTest 环境变量的问题,把环境变量设置好了就可以了 ...
- java报错:Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ss ...
- Java常见异常:Exception in thread "main" java.lang.NoClassDefFoundError
在某一路径下执行编译好的class文件出错. 异常如下: E:\liwy>java Test98 Exception in thread "main" java.lang.N ...
- Exception in thread "main" java.lang.UnsupportedClassVersionError: com/crack
执行一个jar文件的时候抛异常了 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/cr ...
- CXF安装和配置时出现Exception in thread "main" java.lang.UnsupportedClassVersionError:异常?
异常信息: C:\Users\>wsdl2java -h Exception in thread "main" java.lang.UnsupportedClassVersi ...
- 【异常】idea执行Main方法出现 Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
一.异常复现步骤 1)首先得是一个Spring MVC项目 注:Spring Boot项目有内置的web 容器,不会出现该问题 2)main方法存在于使用HttpServletRequest类的类中 ...
- Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...
- unit测试出现异常:Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util
在进行单元测试时,测试出现异常 Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform ...
- Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException
hbase 执行批量删除时出现错误: Exception in thread "main" java.lang.UnsupportedOperationException at j ...
随机推荐
- CF1547B Alphabetical Strings 题解
Content 我们有一个空的字符串,第 \(i\) 次操作我们可以将字母表中第 \(i\) 个字母加入字符串的最前面或最后面.我们称一个长度为 \(n\) 的字符串是合法的,当且仅当这个字符串可以通 ...
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
- 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- treecnt 算法马拉松20(告别美国大选及卡斯特罗)
treecnt 基准时间限制:1 秒 空间限制:131072 KB 给定一棵n个节点的树,从1到n标号.选择k个点,你需要选择一些边使得这k个点通过选择的边联通,目标是使得选择的边数最少. 现需要计算 ...
- python学习第二天:命令行模式和python交互模式
命令行模式 安装完python开发环境和工具之后,在Windows开始菜单选择"命令提示符",就会进入到命令行模式: 或者都可以,然后 点击enter键,弹出下图中的窗口,即命令行 ...
- 【kafka学习笔记】PHP接入kafka
安装扩展 # 先安装rdkfka库文件 git clone https://github.com/edenhill/librdkafka.git 或者: wget https://gitee.com/ ...
- 第二十六个知识点:描述NAF标量乘法算法
第二十六个知识点:描述NAF标量乘法算法 NAF标量乘法算法是标量乘法算法的一种增强,该算法使用了非邻接形式(Non-Adjacent Form)表达,减少了算法的期望运行时间.下面是具体细节: 让\ ...
- 「算法笔记」Link-Cut Tree
一.简介 Link-Cut Tree (简称 LCT) 是一种用来维护动态森林连通性的数据结构,适用于动态树问题. 类比树剖,树剖是通过静态地把一棵树剖成若干条链然后用一种支持区间操作的数据结构维护, ...
- A Primer on Domain Adaptation Theory and Applications
目录 概 主要内容 符号说明 Prior shift Covariate shift KMM Concept shift Subspace mapping Wasserstein distance 应 ...
- [C++]C++四舍五入保留到n位小数
#include <sstream> #include <iostream> #include <iomanip> using namespace std; /** ...