H_蜡笔不小心 2021-12-13 14:35 采纳率: 57.1%
浏览 666
已结题

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

问题遇到的现象和发生背景

java编写魔板游戏

问题相关代码,请勿粘贴截图
package ch7.data;
import javax.swing.*;
import java.awt.*;
public class Block extends JTextField{
    Point point;
    Object object;
    Point [] [] allPoint;
    int index_i,index_j;
    public Block(){
        setEditable(false);
        setHorizontalAlignment(JTextField.CENTER);
        setFont(new Font("Arial",Font.BOLD,16));
        setForeground(Color.blue);
    }
    public void setAtPoint(Point p){
        point = p;
    }
    public void setAllPoint(Point [] [] point){
        allPoint = point;
    }
    public Point getAtPoint(){
        return point;
    }
    public void setObject(Object object){
        this.object = object;
        if(object instanceof Integer){
            Integer number = (Integer)object;
            setText(""+number.intValue());
        }
        else if(object instanceof Image){
            repaint();
        }
    }
    public Object getObject(){
        return object;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);    
        int w=getBounds().width;
        int h=getBounds().height;
        try{
            g.drawImage((Image)object,0,0,w,h,this);
        }
        catch(Exception exp){}
    }
    public boolean move(){
        int m = -1,n=-1;
        boolean successMove = false;
        Point pStart = getAtPoint();
        findIndex(pStart,allPoint);
        for(int i = 0;i<allPoint.length;i++){
            for(int j = 0;j<allPoint.length;i++){
                if(!allPoint[i][j].isHaveBlock()){
                    m = i;
                    n = j;
                }
            }
        }
        if(Math.abs(index_i-m)+Math.abs(index_j-n) == 1){
            this.setAtPoint(allPoint[m][n]);
            successMove = true;
            allPoint[m][n].setBlock(this);
            allPoint[m][n].setHaveBlock(true);
            pStart.setHaveBlock(false);
            pStart.setBlock(null);
        }
        return successMove;
    }
    private void findIndex(Point p,Point[][] allPoint){
        for(int i = 0;i<allPoint.length;i++){
            for(int j = 0;j<allPoint[i].length;j++){
                if(p == allPoint[i][j]){
                    index_i = i;
                    index_j = j;
                    break;
                }
            }
        }
    }
}




```java
package ch7.test;
import ch7.data.*;
public class AppTest{
    public static void main(String [] args){
        Point [] [] point = new Point[3][3];
        for(int i=0;i<point.length;i++){
            for(int j = 0;j<point[i].length;j++)
                point[i][j] = new Point();
        }
        Block block [] [] = new Block[3][3];
        for(int i=0;i<block.length;i++){
            for(int j = 0;j<block[i].length;j++){
                block[i][j] = new Block();
                block[i][j].setAllPoint(point);
                block[i][j].setAtPoint(point[i][j]);
                point[i][j].setHaveBlock(true);
                point[i][j].setBlock(block[i][j]);
            }
        }
        point[2][2].setHaveBlock(false);
        point[2][2].setBlock(null);
        VerifySuccess verifySuccess = new VerifySuccess();
        Integer [] number = {1,2,3,4,5,6,7,8};
        verifySuccess.setPoint(point);
        verifySuccess.setObject(number);
        block[0][0].setObject(number[0]);
        block[0][1].setObject(number[1]);
        block[0][2].setObject(number[2]);
        block[1][0].setObject(number[3]);
        block[1][1].setObject(number[7]);
        block[1][2].setObject(number[4]);
        block[2][0].setObject(number[6]);
        block[2][1].setObject(number[5]);
        intput(point);
        System.out.println("----------------------------");
        System.out.println("移动2次:");
        System.out.println(point[2][1].getBlock().move());
        System.out.println(point[1][1].getBlock().move());
        intput(point);
        System.out.println("成功否"+verifySuccess.isSuccess());
        System.out.println("再移动2次:");
        System.out.println(point[1][2].getBlock().move());
        System.out.println(point[2][2].getBlock().move());
        intput(point);
        System.out.println("成功否"+verifySuccess.isSuccess());
    }
    static void intput(Point [] [] point){
        int k = 0;
        for(int i=0;i<point.length;i++){
            for(int j=0;j<point[i].length;j++){
                String s ="";
                Block bk= point[i][j].getBlock();
                if(bk!=null){
                    Integer object = (Integer)bk.getObject();
                    s =object.toString();
                }
                else
                    s ="#";
                System.out.printf("%5s",s);
            }
            System.out.println();
        }
    }
}

运行结果及报错内容

报错:

img

  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2021-12-13 14:35
    关注

    ArrayIndexOutOfBoundsException: 4
    数组越界异常了。看下控制台具体哪一行代码抛出来的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月13日
  • 已采纳回答 12月13日
  • 修改了问题 12月13日
  • 创建了问题 12月13日

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?