繁体   English   中英

Java数组越界找不到索引越界

[英]Java array out bounds can't find the index out of bounds

这是一个eulers方法程序,我想我已经知道了,但是我一直在让arrayIndex超出我的x和y数组及其计数器之间的界限,我想我知道什么arrayIndex超出界限的意思,但是我似乎无法得到索引超出范围的地方。 有人可以帮忙吗?

import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;

public class INFINITE_EULER {

    /**
     * @param args
     */
    public static float functionof(float b,float c){
        return (float) ((float) Math.pow(b+0.1, 2)+ Math.pow(c+0.1, 2)); //to return the function of x and y 
    }

    public static void main(String[] args) {
        Scanner myScanner = new Scanner(in);
        out.println("Programme to implement Eulers method");
        float h;

        float y[] = new float[100]; //initialize the value of x from 0 to 100
        float x[] = new float[100]; // initialize the value of y from 0 to 100
        int i; //variable i is the counter for the array
        out.println("enter the value of h");
        h = myScanner.nextFloat();
        out.println("Enter the first and second interval");
        x[0]=myScanner.nextFloat(); //take the value of x0
        y[0]=myScanner.nextFloat(); //take the value of y0

        for(i = 0 ; i < 100 ; i ++);{ // for x0 to x100 
            y[i+1] = y[i] + h * Math.abs(functionof(x[i],y[i])); //do yi+1 = yi + h * function of current x and current y through the loop
            out.print("y");
            out.print(i);
            out.print("=");
            out.print(y[i]);
        }
    }
}

您的问题:在

  y[i+1] = y[i] + h * Math.abs(functionof(x[i],y[i])); //do yi+1 = yi + h * function of current x    and  current y through the loop

循环时

for(i = 0 ; i < 100 ; i ++)

意思是

 for(i=0 to 99) 
 y[i+1] -- > when i=99, you will try to acess y[99+1] i.e, y[100] that doesn't exist

编辑:将您的代码更改为:

for(i = 1 ; i < 100 ; i ++){ // for x1 to x99
        y[i] = y[i-1] + h * Math.abs(functionof(x[i-1],y[i-1])); 
        out.print("y");
        out.print(i-1);
        out.print("=");
        out.print(y[i-1]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM