java lang ArrayIndexOutOfBound ... [英] java lang ArrayIndexOutOfBound...

查看:77
本文介绍了java lang ArrayIndexOutOfBound ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助解决这个异常。

下面的代码将帮助我从文件中获取时间戳,如果其中包含所需的字符串Stopping Component。 />


Can anyone help in solving this exception pls.
The below code will help me in getting the time stamp from a file, if the required string "Stopping Component"is present in it.

private String[] startTime(String [] startTime) throws Exception {
		try {
			String Log = Log(); /// this is a method which will read a file
			String [] columns = Log.split(" ");
			String [] startTime1 = new String [3] ;
			int j = 0;
			for (int i= 0; i < Log.length(); i++) {
				if (columns[i].contains("Stopping") && columns[i+1].contains("Component")) {
					startTime1[j] = columns[i+2];

					System.out.println("The start time in method "  + startTime1[j]);
					j++;
				}
			}
			
			return startTime1;
		} catch (Exception e) {
			System.out.println(" exception occurred" + e );
			return null;
		}

	}







跑步时我得到了以下异常..我看到两个时间戳被提取..但是我得到了outOfBoundsException。



我从网上发现当我们发生这个异常时尝试访问数组的非法索引。但是我无法看到上面代码中的内容。请指导我



输出:

方法05:32的开始时间:17.516

开始时间在方法05:32:23.242

异常发生java.lang.ArrayIndexOutOfBoundsException:1854443




when ran I am getting the below exception..I see that the two time stamps are fetched.. but i got the outOfBoundsException.

I found from the net that this exception will occur when we try to access illegal index of an array. But I am unable to see what is that here in above code. Please guide me

output:
The start time in method 05:32:17.516
The start time in method 05:32:23.242
exception occurred java.lang.ArrayIndexOutOfBoundsException: 1854443

推荐答案

你怎么知道<$ c时$ c> startTime1 [j] , j 总是低于3?

你怎么知道 columns [i + 2] 总是存在吗?

你循环上 i 这是有限的大小 log ,但是在循环中你可以在不知道大小的情况下访问 columns 元素。



您应该尝试使用调试器来查看您的代码是什么。
How do you know that when startTime1[j], j is always below 3 ?
How do you know that columns[i+2] always exist ?
You loop on i which is bounded to the size of log, but in the loop you access to columns element without knowing the size.

You should try the debugger to see what is doing your code.


为什么不尝试防御性编码。我相信变量j可以导致非法索引,这意味着在某些时候代码试图访问内存中不存在的startTime1 [3],不幸的是Java会抛出异常。而且i + 2有可能访问索引界限。



我的建议是使用和if语句



Why not try defensive coding. I believe the variable j can cause the illegal index which means at some point the code is trying to access startTime1[3] which is not present in memory and unfortunately Java will throw exception.Also i+2 has the potential to access index out of bounds.

My advice would be to use and if statement

if(j<3 && i<log.length()-2)>   startTime1[j] = columns[i+2];
}





希望这能解决你的问题.....



PS.-如果你想要线程安全的话,不要使用它们是不可变的字符串使用StringBuilder或StringBuffer ....



Hope this solves you problem.....

PS.- Do not use Strings they are immutable use StringBuilder or StringBuffer if you want thread safe....


这篇关于java lang ArrayIndexOutOfBound ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆