如何在java中演示2 d矩阵? [英] How to demonstrate 2 d matrix in java?
问题描述
问题出在输出中显示1个我不理解的错误..
哪里出错?
我是什么尝试过:
The problem is in output showing 1 error that I donot understand ..
Where is error?
What I have tried:
import java.util.*;
/*Please dont change class name, Dcoder
and class must not be public*/
//Compiler version JDK 1.8
//output of the given matrix is // 0 1 2 3 4
// 5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
class Dcoder
{
public static void main(String args[])
{
int I,j,k=0;
int A[][]=new int[4][];
for(I=0;I<4;I++){
for(j=0;j<5;j++){
A[I][j]= k;
k++;
}}
for(I=0;I<4;I++){
for(j=0;j<5;j++){
System.out.println("" + A[I][j]);
System.out.println();
}}
}
}
推荐答案
看看你的代码:
Look at your code:
int A[][]=new int[4][];
for(I=0;I<4;I++){
for(j=0;j<5;j++){
A[I][j]= k;
k++;
}}
让我们开始让它更具可读性,是吗?
And let's start by making it a little more readable, shall we?
int a[][] = new int[4][];
for(i = 0; i < 4; i++){
for(j = 0; j < 5; j++){
a[i][j] = k;
k++;
}
}
在变量命名中保持一致:局部变量应该是小写,而不是混合,让你的缩进正确!它使你的代码整个加载更容易阅读...
您声明2D数组,但您只指定一个大小:因此系统不会为第二维分配任何内存。没有记忆,当你试图访问它时它会失败。
试试这个:
Be consistent in your variable naming: local variables should be lowercase, not a mixture, and get your indentation correct! It makes your code a whole load easier to read...
You declare the 2D array, but you only specify one size: so the system doesn't allocate any memory for the "second dimension". No memory, and it will fail when you try to access it.
Try this:
int a[][] = new int[4][5];
它应该让你更进一步。
这篇关于如何在java中演示2 d矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!