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

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日

悬赏问题

  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)