Java 多维数组初始化有问题

Java 多维数组初始化有问题,java,Java,我正在努力学习java,当然我是个初学者。我在初始化多维数组时遇到问题。这是我要写的代码 import java.util.Scanner; public class one { public static void main(String args[]) { int p[][] = null; System.out.println("Type ur array here:"); System.out.println("how many

我正在努力学习java,当然我是个初学者。我在初始化多维数组时遇到问题。这是我要写的代码

import java.util.Scanner;
public class one {

    public static void main(String args[]) {
        int p[][] = null;
        System.out.println("Type ur array here:");
        System.out.println("how many rows and column:");
        int row, colmn;
        Scanner u = new Scanner(System.in);
        Scanner y = new Scanner(System.in);
        Scanner t = new Scanner(System.in);
        Scanner r = new Scanner(System.in);

        row = t.nextInt();
        colmn = r.nextInt();

        for(int i = 0; i <= row; i++)
            for(int v = 0; v <= colmn; v++){
                int j = u.nextInt();
                p[row][colmn] = j;
            }

        int a[][] = p;
        System.out.println("The given array:");
        y(a);

    }
    public static void y(int n[][]) {

        for(int i=0;i<n.length;i++) {
            for(int j=0;j<n[i].length;j++){
                System.out.print(n[i][j]);
            }
            System.out.println();
        }
    }

}
import java.util.Scanner;
公共一级{
公共静态void main(字符串参数[]){
int p[][]=null;
System.out.println(“在此处键入ur数组:”);
System.out.println(“多少行和多少列:”);
int行,colmn;
扫描器u=新扫描器(System.in);
扫描器y=新扫描器(System.in);
扫描器t=新扫描器(System.in);
扫描仪r=新的扫描仪(System.in);
row=t.nextInt();
colmn=r.nextInt();

对于(int i=0;i,注释中提到了对代码的更改,但请参见以下代码:

import java.util.Scanner;

public class one {

    public static void main(String args[]) {
        int p[][] = null;
        System.out.println("Type ur array here:");
        System.out.println("how many rows and column:");
        int row, colmn;
        Scanner u = new Scanner(System.in);

        // Only one is required to read from standard input stream, instead of:
        // Scanner y = new Scanner(System.in);
        // Scanner t = new Scanner(System.in);
        // Scanner r = new Scanner(System.in);

        // Use Scanner object "u":
        row = u.nextInt();
        colmn = u.nextInt();

        // Memory to array:
        p = new int[row][colmn];

        // Change '<=' to '<' as arrays are 0 index and will give index out of bounds exception ;
        // Or change 'p = new int[row][colmn];' to 'p = new int[row + 1][colmn + 1];'.
        for(int i = 0; i < row; i++){
            for(int v = 0; v < colmn; v++){
                int j = u.nextInt();
                // Change indices to "i, v" instead of "row, colmn":
                p[i][v]=j;
            }

        }
        // Bad way to copy array as same reference is going to be used.
        // To copy array use the following:

        /* 
         * // int a[][] = p;
         * int[][] a = new int[row][colmn];
         * for (int i = 0; i < row; i++) {
         *     System.arraycopy(p[i], 0, a[i], 0, colmn);
         * }
         */

        int a[][] = p;
        System.out.println("The given array:");
        y(a);

    }
    public static void y(int n[][]){

        for(int i = 0; i < n.length; i++){
            for(int j = 0; j < n[i].length; j++)
                System.out.print(n[i][j]);
            System.out.println();
        }   
    }

 }
import java.util.Scanner;
公共一级{
公共静态void main(字符串参数[]){
int p[][]=null;
System.out.println(“在此处键入ur数组:”);
System.out.println(“多少行和多少列:”);
int行,colmn;
扫描器u=新扫描器(System.in);
//从标准输入流读取数据只需要一个,而不是:
//扫描器y=新扫描器(System.in);
//扫描器t=新扫描器(System.in);
//扫描仪r=新的扫描仪(System.in);
//使用扫描仪对象“u”:
行=u.nextInt();
colmn=u.nextInt();
//内存到阵列:
p=新整数[行][列];

//更改“您可以使用
int[]]p=new int[capacity][capacity]初始化多维数组;
@August或
int[]]p=new int[capacity][]
来代替。请注意,第二个容量可以保留为空,在这种情况下,您可以使用
p[index]=new int[capacity]自由建立第二个多维数组
。另外,请使用描述性变量名。4个不同的扫描器具有相同的输入源?只需使用一个即可。另外
p[row][colmn]=j;
将覆盖前一次迭代的内容。使用
p[i][v]=j
相反。别忘了像August建议的那样初始化数组。将完整的异常堆栈跟踪…发布到问题中