Java 三维数组索引超出范围
这是我的第一个问题。我正在尝试编写一个程序,它读取的数组数与我从键盘输入的数组数相同。输入列数后,我得到错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:0 提前谢谢Java 三维数组索引超出范围,java,arrays,indexoutofboundsexception,multidimensional-array,Java,Arrays,Indexoutofboundsexception,Multidimensional Array,这是我的第一个问题。我正在尝试编写一个程序,它读取的数组数与我从键盘输入的数组数相同。输入列数后,我得到错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:0 提前谢谢 package Final; import java.util.*; public class Final { int a,b,x,l; double mat[][][]; Final() { a=0;
package Final;
import java.util.*;
public class Final {
int a,b,x,l;
double mat[][][];
Final()
{
a=0;
b=0;
c=0;
d=0;
x=0;
l=0;
}
public void read() {
Scanner citast = new Scanner(System.in);
System.out.println("How many arrays do you want to enter?");
x=citast.nextInt();
for (this.l=0; this.l<x; this.l++)
{
System.out.println("Matrix "+this.l);
System.out.println("Number of lines: ");
this.a = citast.nextInt();
System.out.println("Number of columns: ");
this.b = citast.nextInt();
this.mat= new double [this.l][a][b];
for (int i=0; i<this.a; i++)
{
for (int j=0; j<this.b; j++)
{
System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
this.mat[l][i][j]=citast.nextInt();
}
System.out.println();
}
}
}
public static void main(String[] args) {
Final a = new Final();
Scanner citast = new Scanner(System.in);
a.read();
System.out.println();
}
}
package-Final;
导入java.util.*;
公开课决赛{
int a,b,x,l;
双垫[];
最后()
{
a=0;
b=0;
c=0;
d=0;
x=0;
l=0;
}
公共无效读取(){
扫描仪citast=新扫描仪(System.in);
System.out.println(“要输入多少数组?”);
x=citast.nextInt();
对于(this.l=0;this.l
应该是:
this.mat= new double [x][a][b];
但是您的代码是错误的,您应该在第一个for循环之前设置数组的维度:
public void read() {
Scanner citast = new Scanner(System.in);
System.out.println("How many arrays do you want to enter?");
x=citast.nextInt();
this.mat = new double[x][][]; //<-- added this line
for (this.l=0; this.l<x; this.l++)
{
System.out.println("Matrix "+this.l);
System.out.println("Number of lines: ");
this.a = citast.nextInt();
System.out.println("Number of columns: ");
this.b = citast.nextInt();
this.mat[l] = new double [a][b]; //<-- modified this line
for (int i=0; i<this.a; i++)
for (int j=0; j<this.b; j++)
{
System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
this.mat[l][i][j]=citast.nextInt();
}
System.out.println();
}
System.out.println(Arrays.deepToString(mat));
}
应该是:
this.mat= new double [x][a][b];
但是您的代码是错误的,您应该在第一个for循环之前设置数组的维度:
public void read() {
Scanner citast = new Scanner(System.in);
System.out.println("How many arrays do you want to enter?");
x=citast.nextInt();
this.mat = new double[x][][]; //<-- added this line
for (this.l=0; this.l<x; this.l++)
{
System.out.println("Matrix "+this.l);
System.out.println("Number of lines: ");
this.a = citast.nextInt();
System.out.println("Number of columns: ");
this.b = citast.nextInt();
this.mat[l] = new double [a][b]; //<-- modified this line
for (int i=0; i<this.a; i++)
for (int j=0; j<this.b; j++)
{
System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
this.mat[l][i][j]=citast.nextInt();
}
System.out.println();
}
System.out.println(Arrays.deepToString(mat));
}
这应该行得通。
首先创建一个大小为x
的数组,以容纳任意大小的二维数组。
然后,为每个l
创建一个大小为a
xb
的二维数组,将其放置在mat
的l
位置,并读取其中的值
Scanner citast = new Scanner(System.in);
System.out.println("How many arrays do you want to enter?");
x=citast.nextInt();
this.mat = new double[x][][];
for (this.l=0; this.l<x; this.l++) {
System.out.println("Matrix "+this.l);
System.out.println("Number of lines: ");
this.a = citast.nextInt();
System.out.println("Number of columns: ");
this.b = citast.nextInt();
this.mat[this.l] = new double [a][b];
for (int i=0; i<this.a; i++)
for (int j=0; j<this.b; j++)
{
System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
this.mat[l][i][j]=citast.nextInt();
}
System.out.println();
}
}
Scanner-citast=新扫描仪(System.in);
System.out.println(“要输入多少数组?”);
x=citast.nextInt();
this.mat=新的双精度[x][];
对于(this.l=0;this.l这应该有效。
首先创建一个大小为x
的数组,以容纳任意大小的二维数组。
然后,为每个l
创建一个大小为a
xb
的二维数组,将其放置在mat
的l
位置,并读取其中的值
Scanner citast = new Scanner(System.in);
System.out.println("How many arrays do you want to enter?");
x=citast.nextInt();
this.mat = new double[x][][];
for (this.l=0; this.l<x; this.l++) {
System.out.println("Matrix "+this.l);
System.out.println("Number of lines: ");
this.a = citast.nextInt();
System.out.println("Number of columns: ");
this.b = citast.nextInt();
this.mat[this.l] = new double [a][b];
for (int i=0; i<this.a; i++)
for (int j=0; j<this.b; j++)
{
System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
this.mat[l][i][j]=citast.nextInt();
}
System.out.println();
}
}
Scanner-citast=新扫描仪(System.in);
System.out.println(“要输入多少数组?”);
x=citast.nextInt();
this.mat=新的双精度[x][];
对于(this.l=0;this.l
出了它,它就开始工作了
此外,还必须将Final()设为静态,并在类中声明c和d,因为它们在构造函数中,但在类中没有对它们的引用
出了它,它就开始工作了
您还必须将Final()设为静态,并在类中声明c和d,因为它们在构造函数中,但在类中没有对它们的引用。使用Scanner citast=new Scanner(System.in)有什么意义
在main中?尝试给有意义的变量命名,如行高列定位器…您真的想为每个l允许不同大小的矩阵吗?@INIX软件代码在这里编辑,我在程序中有更多的函数,我忘了删除扫描仪。@iShaalan谢谢您的提示。Scanner citast=新扫描器(System.in);
在main中?尝试给有意义的变量命名,如行高columnsIterator…您真的想为每个l允许不同大小的矩阵吗?@INIX软件在这里编辑了代码,我在程序中有更多的函数,我忘了删除扫描器。@iShaalan感谢您的提示。
this.mat[l][i][j] = citast.nextInt();