简体   繁体   中英

Index 10 out of bounds for length 10 - why?

When I am running my program it terminates after following the upper steps and inputs. After entering the following output appears: `

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
    at Program.main(Program.java:30)

`

My goal was to create a program which shows any graph on a coordinate system. With a 2d array and the input of x and y I tried to create a grid system, that's my code: `

import java.util.Scanner;

public class Program {

    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        
        int m, xrange, yrange;
        char ys, xs;
        
        System.out.println("You have chosen the program 'ShowMyGraph'.");
        System.out.println("Now please cosider following the steps in order to enter your function which ought to be printed on the console.");
        System.out.print("First, how long do you want your coordinate system to be? Enter the x range: ");
        xrange = sc.nextInt();
        System.out.print("The y range: ");
        yrange = sc.nextInt();
        System.out.print("Enter the graph increase m: ");
        m = sc.nextInt();
        System.out.print("In case there is a y shift enter it, otherwise press 'n'.");
        ys = sc.next().charAt(0);
        if (ys == 'n') ys = 0;
        System.out.println("In case there is a x shift enter it, otherwise press 'n'.");
        xs = sc.next().charAt(0);
        if (xs == 'n') xs = 0;
        
        char [][] GridSystem = new char [yrange*2][xrange*2];
        
        for (int x = xrange; x < xrange*2+2; x++) {
                GridSystem[yrange][x] = '-';
                if (x==xrange+2) GridSystem[yrange][x] = '>';
            }
        
        for (int y = 0; y < yrange*2+2; y++) {
                GridSystem[y][xrange] = '|';
                if (y==yrange*2+2) GridSystem[y][xrange] = '^';
        }
        
        
        for (int i = 0; i < GridSystem.length; i++) {
            for (int j = 0; j < GridSystem[i].length; j++) {
                System.out.print(GridSystem[i][j]);
            }
            System.out.println();
        }
        
    }

}

What was my mistake? Should I use a file to import or was the basic idea ok?

    for (int x = xrange; x < xrange*2+2; x++) {
            GridSystem[yrange][x] = '-';
            if (x==xrange+2) GridSystem[yrange][x] = '>';
        }
    
    for (int y = 0; y < yrange*2+2; y++) {
            GridSystem[y][xrange] = '|';
            if (y==yrange*2+2) GridSystem[y][xrange] = '^';
    }

In this functions you loop two additional times through. This creates indexes that are out of the Array bounderies. To make sure they are in the boundaries you would need to remove the +2 in the condition or create a larger array in the beginning including the +2 like:

char [][] GridSystem = new char [yrange*2+2][xrange*2+2];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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