如何调试“java.lang.ArrayIndexOutOfBoundsException”在Java程序中? [英] How to debug "java.lang.ArrayIndexOutOfBoundsException" in a Java program?

查看:84
本文介绍了如何调试“java.lang.ArrayIndexOutOfBoundsException”在Java程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习Java,我的代码也有一些问题。好吧,任务是得到一个数字并打印这样的矩阵:

I've just started to learn Java,and I've got some questions for my code. Well,the task is to get a number and print a matrix like this:

get 3,and print:
    1 2 3
    8 9 4
    7 6 5

get 4,and print:
    1  2  3  4
    12 13 14 5
    11 16 15 6
    10 9  8  7

因为我正在学习算法,所以我通过DFS处理问题,这是我的cpp代码,它可以解决任务。

Because I'm learning algorithm,so I deal the question by DFS, here's my cpp code,which can solve the task.

#include "iostream"
using namespace std;
int n;
int matrix[100][100]={0};
int visited[100][100]={0};


void dfs(int n,int x,int y,int i,int dire)
{
//x,y is the coordinate,i is the number to write
//dire is the variety to control the direction of the dfs
    if(i==n*n+1)return;
    switch(dire)
    {
        case(1)://write the number on right
        {
            if(visited[x][y+1]==0&&y+1<n)
            {
                visited[x][y+1]=1;
                matrix[x][y+1]=i;
                i++;
                dfs(n,x,y+1,i,1);
            }
            else dfs(n,x,y,i,2);
            break;
        }
        case(2)://down
        {
            if(visited[x+1][y]==0&&x+1<n)
            {
                visited[x+1][y]=1;
                matrix[x+1][y]=i;
                i++;
                dfs(n,x+1,y,i,2);
            }
            else dfs(n,x,y,i,3);
            break;
        }
        case(3)://left
        {
            if(visited[x][y-1]==0&&y-1>=0)
            {
                visited[x][y-1]=1;
                matrix[x][y-1]=i;
                i++;
                dfs(n,x,y-1,i,3);
            }
            else dfs(n,x,y,i,4);
            break;
        }
        case(4)://up
        {
            if(visited[x-1][y]==0&&x-1>=0)
            {
                visited[x-1][y]=1;
                matrix[x-1][y]=i;
                i++;
                dfs(n,x-1,y,i,4);
            }
            else dfs(n,x,y,i,1);
            break;
        }
    }
}

 void output(int n)
{
    int p,q=0;
    for(q=0;q<n;q++)
    {
        for(p=0;p<n;p++)
        {
            cout<<matrix[q][p]<<" ";
            if(matrix[q][p]<10)
                cout<<" ";
            if(matrix[q][p]<100)
                cout<<" ";
        }
        cout<<endl;
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            matrix[i][j]=0;
            visited[i][j]=0;
        }
    }
    ///dfs_r(n,0,-1,1);
    dfs(n,0,-1,1,1);
    output(n);
}

但是当我把它翻译成Java时出现问题

But things goes wrong when I translate it into Java

import java.util.Scanner;
public class mainclass{
    public static class method{
        int n;
        int visited[][]=new int[n][n];
        int matrix[][]=new int[n][n];
        method(int temp){
            n=temp;
        }
        void dfs(int x,int y,int i,int dire)
        {
            if(i==n*n+1)return;
            switch(dire)
            {
                case(1):
                {
                    if(visited[x][y+1]==0&&y+1<n)
                    {
                        visited[x][y+1]=1;
                        matrix[x][y+1]=i;
                        i++;
                        dfs(x,y+1,i,1);
                    }
                    else dfs(x,y,i,2);
                    break;
                }
                case(2):
                {
                    if(visited[x+1][y]==0&&x+1<n)
                    {
                        visited[x+1][y]=1;
                        matrix[x+1][y]=i;
                        i++;
                        dfs(x+1,y,i,2);
                    }
                    else dfs(x,y,i,3);
                    break;
                }
                case(3):
                {
                    if(visited[x][y-1]==0&&y-1>=0)
                    {
                        visited[x][y-1]=1;
                        matrix[x][y-1]=i;
                        i++;
                        dfs(x,y-1,i,3);
                    }
                    else dfs(x,y,i,4);
                    break;
                }
                case(4):
                {
                    if(visited[x-1][y]==0&&x-1>=0)
                    {
                        visited[x-1][y]=1;
                        matrix[x-1][y]=i;
                        i++;
                        dfs(x-1,y,i,4);
                    }
                    else dfs(x,y,i,1);
                    break;
                }
            }
        }
        void output()
        {
            int p,q=0;
            for(q=0;q<n;q++)
            {
                for(p=0;p<n;p++)
                {
                    System.out.print(matrix[q][p]);
                    if(matrix[q][p]<10)
                        System.out.print(" ");
                    if(matrix[q][p]<100)
                        System.out.print(" ");
                }
                System.out.println();
            }
        }
    }

    public static void main(String args[]){
        Scanner reader=new Scanner(System.in);
        int n=reader.nextInt();
        method c=new method(n);
        c.dfs(0,-1,1,1);
        c.output();
    }
}

我认为我的算法没问题,但我可以解决为什么我的Java代码崩溃了。我想我应该更多地了解Java的基本语法。

I think my algorithm is all right,but I can't work it out why my Java code crashes. I think I should learn more about basic grammar of Java.

编辑:
我使用dev-cpp来运行我的cpp代码和eclipse来运行我的Java代码。我真的不知道如何描述Eclipse的崩溃是如何得到的,代码是:

edit: I use dev-cpp to run my cpp code and eclipse to run my Java code. I'm not really know how to describe how does the crash of my Eclipse got, the code is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

at mainclass$method.dfs(mainclass.java:17)

at mainclass.main(mainclass.java:87)


<terminated, exit value: 1>C:\Program Files\Java\jre1.8.0_101\bin\javaw.exe (2016年9月27日 上午9:51:32)  

坠机现场

推荐答案

(答案有假设你有一个IDE,比如 Eclipse )。

(The answer there supposes you have an IDE, like Eclipse).

如果有疑问,你可以依赖一个好的旧的 System.out.println (yourobject)。小心对象,永远不要忘记你可以将它们变成 String 并检查内部对象(在你的情况下, visited [1] [2] matrix [2] [0] 例如)。 很糟糕。真。它可能足以满足您的需求,但它确实是一个坏习惯。

You can, when in doubt, always rely on a good old System.out.println(yourobject). Be careful about objects, never forget that you can turn them into String and check inner objets (in your case, visited[1][2] or matrix[2][0] for example). BUT it sucks. Really. It might suffice for your needs, but it is really a bad habit to take.

使用名为 SLF4J 的工具获取一些额外的痕迹。由于你只是一个小方法,而不是一个完整的项目,我不会命令你部署这个工具,但请记住,这是一个很好的方法来弄清楚发生了什么,让你的控制台记录你需要的,特别是当项目有进展时(阅读:更大)。

Use a tool named SLF4J to get some additional traces. As you are only on a small method and not a complete project, I would not command you to deploy this tool yet, but keep in mind that it is a good way to both figure out what happens and have your console logging what you need, especially when the projects are getting intersting (read: bigger).

了解一下名为调试模式的工具。你可以找到更多帮助在Eclipse帮助中心,但基本上步骤如下:

Learn a bit about a tool called debug mode. You can find further help on the Eclipse help center, but basically the steps are:


  • 放置一个断点。在代码的左侧(包含行号),您可以右键单击以设置/启用/禁用断点。

  • debug 。这是经典运行图标附近的一个小错误(多么方便)。您将被重定向到调试视角

  • 运行将中断您转到的地方断点。在那里,您将能够一步一步地进入您的代码。您还可以检查您声明的每个变量的状态,以确定您的问题。

  • [您的工作是进行必要的更改]

  • 现在您了解调试器, ArrayOutOfBoundsException 并且由于这种良好做法而成为一个更好的人。

  • Place a breakpoint. On the left of your code (with line numbers), you can right-click to toogle / enable / disable breakpoints.
  • Run your program in debug. It is a little bug (how convenient) near the classic run icon. You'll get redirected to the debug perspective.
  • The run will be interrupted where you toogled your breakpoint. There, you will be able to go, step by step, into your code. You will also be able to check the state of every variable you declared, in order to identify your problem.
  • [Your job here is to make the necessary changes]
  • Now you know about debugger, ArrayOutOfBoundsException and became a better person in general thanks to this good practice.

Vogella 好的教程关于如何以一种很好的方式使用调试器。如果你计划继续使用Java,请学会使用它,因为这会节省你很多时间(非常多,相信我)。

Vogella has a good tutorial about how to use the debugger in a nice way. Learn to use it if you plan on keeping on Java, because this will save you a lot of time (an awful lot, believe me).

这篇关于如何调试“java.lang.ArrayIndexOutOfBoundsException”在Java程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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