Stars.java arrayindexoutofbounds
我一直在试图找出为什么我已经得到这个错误将近3个小时,我需要一些帮助,以得到这个之前,我的电脑支付的价格 我一直在犯这个错误Stars.java arrayindexoutofbounds,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我一直在试图找出为什么我已经得到这个错误将近3个小时,我需要一些帮助,以得到这个之前,我的电脑支付的价格 我一直在犯这个错误 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:13 在StarsTable.checkIfStar(StarsTable.java:102) 在Stars.main(Stars.java:20) 导入java.io.BufferedReader; 导入java.io.FileReader; 导入java.io.
线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:13
在StarsTable.checkIfStar(StarsTable.java:102)
在Stars.main(Stars.java:20)
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.*;
公共类星宿
{
私有ArrayList a=新ArrayList();
私有字符串标题;
私有字符串readLine=null;
私有int行=0;
专用整数数组[];
公共StarsTable(字符串文件名)
{
尝试
{
BufferedReader br=新
BufferedReader(新文件读取器(文件名));
title=br.readLine();
而((readLine=br.readLine())!=null)
{
System.out.println(读线);
addArray(readLine);
行++;
}
br.close();
}
捕获(IOE异常)
{
System.err.println(“未找到文件,请”
+“在命令行中输入文件名。”);
系统出口(1);
}
}
公共字符串标题()
{
返回标题;
}
公共void addArray(字符串读取行)
{
int i=0;
字符串[]splitLine=readLine.split(\\s+);
对于(i=0;i
}
正在读取的文件类型如下所示
标题行
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3
每个文件的行数和每行的行数未知,文件通过主程序通过命令行读取。您可能希望
==array.length-1
而不是==array.length
,因为array.length
比最大索引大1。这可能也适用于a.size()
,不确定。array.length提供数组的长度。但是,数组从索引0开始,因此数组的最后一个索引将是array.length-1
因此,您的测试应该是:
k == array.length - 1
在您的checkIfStar方法中谢谢。这很有帮助。。。
k == array.length - 1