JAVA Eclipse中的数组索引超出界限 [英] Array Index Out of Bounds in JAVA Eclipse

查看:289
本文介绍了JAVA Eclipse中的数组索引超出界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



请光临我。我知道之前有人问这个问题,但我找不到解决办法。我重新创建的这个示例来自JAVA教程网站。我读了那个并重新创建了它。我不知道如何解决这个问题以及如何

hello!

Please bare with me. I know this question was asked before but I just can`t find a solution. This example that I recreated is taken from a JAVA tutorial website. i read that and recreated it. I have no idea how to fix this and how

args[0]

是Out Of Of界限。



这是代码:



is Out Of Bounds.

Here is the code:

public class Sqrtcalc {
	
	public static void main (String[] args) {
		
		double b = Double.parseDouble(args[0]);
		double c = Double.parseDouble(args[1]);
		double a = Double.parseDouble(args[2]);
		
		double Delta = b*b - 4.0*a*c;
		double sqrootD = Math.sqrt(Delta);
		
		double N1 = (-b + sqrootD)/ (2.0*a);
		double N2 = (-b - sqrootD)/ (2.0*a);
		
		System.out.println(N1);
		System.out.println(N2);
		
	}

}





这就是错误。如果我删除



And this is the Error. If i remove

double b

它只是移动到下一个值(Double c)并且同样的事情。



it just moves to the next value (Double c) and says the same thing.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at Sqrtcalc.main(Sqrtcalc.java:6)





第6行是双b的那个。



感谢您的帮助和时间!



line 6 being the one with Double b.

Thank you for your help and time!

推荐答案

首先,它与Eclipse无关,这是只是IDE。当你运行你的应用程序时,它是不合时宜的。



问题非常简单。变量 args 表示启动应用程序时在命令行中写入的每个字符串数组命令行参数。请参阅: https://en.wikipedia.org/wiki/Entry_point#Java [ args [0] 。



解决方案:经常检查在通过索引访问数组的任何元素之前数组的长度。



(不,在这种特殊情况下不需要调试任何东西;问题是晶莹剔透。:-))



-SA
First of all, it has nothing to do with Eclipse, which is just the IDE. When you run your application, it's out of equation.

The problem is very simple. The variable args means the array of string each representing command-line argument written in the command line when you start the application. Please see: https://en.wikipedia.org/wiki/Entry_point#Java[^].

Now, if you simply start the application with no command line, no arguments, the length of this array is zero. There are no any element, including, of course, args[0].

Solution: always check up the length of an array before accessing any element of an array by index.

(No, there is no a need to debug anything in this particular case; the problem is crystal clear. :-))

—SA


你的问题是 args 不包含您​​的期望。

了解args中的内容的唯一方法是使用调试器运行程序并检查变量,特别是 args



您可以通过检查 args <的大小来使代码更安全/ code>在尝试使用之前。



对于解决方案,您需要为我们提供此代码的调用行。
Your problem is that args do not contain what you expect.
The only way to know what is in args is to run the program with the debugger and inspect the variables, and particularly args.

You can make your code safer by checking the size of args before trying to use it.

For a solution, you need to give us the calling line for this code.


这篇关于JAVA Eclipse中的数组索引超出界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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