Home > Back-end >  Java. Lang. ArrayIndexOutOfBoundsException: Index 0 out
Java. Lang. ArrayIndexOutOfBoundsException: Index 0 out

Time:10-01

Pany package com.com;
//given a number n * m landmines, now please calculate each matrix
//each cell in the adjacent cell in the number of mines, each cell
//up to eight adjacent cell, 0 & lt; N, m<=100
//? Input format:
//input contains a number of matrix, for each matrix, the first line contains two integers n
//and m, respectively the rows and columns of the matrix, the next n lines each line contains m
//characters, safety area with '. ', said a mine area with a '*', said when
//input end when n=m=0,
//? The output format
//for the case of a matrix, I first print the serial number in a separate line: "Field
//# I: ", the next n lines, read the '. 'should be the number of mines around the location
//replaced, the output of every two matrix must be separated by a blank line,
import java.util.Scanner;
Public class Main
{
Public static void main (String [] args) {
Scanner input=new Scanner(System.in);
While (true)
{//input matrix
System. The out. Println (" input of landmines ranks number: ");
int count=0;
System. The out. Print (" number of lines: ");
Int n=input. NextInt ();
System. The out. Print (" the number of columns: ");
Int j=input nextInt ();
If (n==m & amp; & N==0) {
break;
}
count++;
Char [] [] a=new char [n + 1] [m + 1];
System. The out. Println (" input matrix mines: ");
For (int I=0; I & lt; N + 1; I++) {
A [I]=input. NextLine (). ToCharArray ();
}
System. The out. Println (" Field# "+" : "+ count);//output matrix
The deal (a, n, m);
}

}
Private static void deal (char [] [] a, int n, int m)
{
For (int I=0; I & lt; n; I++) {
For (int j=0; J & lt; m; J++) {
If (a [I] [j]=='*') {
System. The out. Print (" * ");
continue;//skip this cycle, the implementation of the next cycle
}
int count=0;
//polling around eight points up and down
For (int k=I - 1; K & lt;=I + 1; K++) {
For (int l=j - 1; L & lt;=j + 1; L++) {
//note that boundary
If (k & gt;=0 & amp; & K & lt; N & amp; & L & gt;=0 & amp; & L & lt; M & amp; & A [k] [l]=='*')
count++;
}
}
System. The out. Print (count);
}
System. The out. Println ();
}

}
}

CodePudding user response:

Said the compiler error is deal function inside the if (a [I] [j]=='*')
The Exception in the thread "main" Java. Lang. ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
Don't understand why @ - @

CodePudding user response:

See n, m value and the length of a two dimensional array
  • Related