如何在Java中获取二维动态数组的输入?

如何在Java中获取二维动态数组的输入?,java,Java,我正在编写一个代码,其中输入将从用户处获取并存储在二维动态数组中。我们知道数组的行数。但是数组的列数是动态的 输入的格式如下所示: 三, sghjhlklj ghgh ytyhuj 由于3是先输入的,因此必须在数组中存储三个后续字符串 下面是我编写的代码片段。但它显示了数组索引越界异常 import java.util.Arrays; import java.util.Scanner; class Main { // Read a String from the standard in

我正在编写一个代码,其中输入将从用户处获取并存储在二维动态数组中。我们知道数组的行数。但是数组的列数是动态的

输入的格式如下所示:

三,

sghjhlklj

ghgh

ytyhuj

由于3是先输入的,因此必须在数组中存储三个后续字符串

下面是我编写的代码片段。但它显示了数组索引越界异常

import java.util.Arrays;
import java.util.Scanner;

class Main
{
    // Read a String from the standard input using Scanner
    public static void main(String[] args)
    {
        
        Scanner in = new Scanner(System.in); 
  
        Integer no = in.nextInt(); 
        System.out.println("You entered string "+no); 
        int z = 0,y=0 ;
        
        char[][] arr = new char[no][];
        
        for(z=0 ; z<no ; z++)
        {
            String s = in.nextLine(); 
            String[] arrOfStr = s.split("");
            for( y =0 ; y<arrOfStr.length ; y++)
            {
                arr[z][y]=arrOfStr[y].charAt(0);
                
            }
            
            
            
        }     
        
    
        in.close();     
    }
}
导入java.util.array;
导入java.util.Scanner;
班长
{
//使用扫描仪从标准输入中读取字符串
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
整数no=in.nextInt();
System.out.println(“您输入的字符串”+否);
int z=0,y=0;
char[][]arr=新字符[no][];

对于(z=0;z您的代码有3个问题:

  • 您从不初始化内部数组。请使用
    arr[z]=new char[s.length()];
    进行初始化
  • <> LI>定义代码> ARROFSTR 。用空白子字符串拆分字符串,只需使用<代码> s <代码>使用<代码> CARAT < /代码>如下:
    arr[z][y]=s.charAt(y);
  • 如注释所示,
    nextInt
    存在未考虑
    \n
    (输入)字符的问题。 因此,使用
    int no=Integer.parseInt(in.nextLine());
    ,而不是使用
    nextInt
  • 最终代码应如下所示:

    for(z=0 ; z<no ; z++)
    {
        String s = in.nextLine(); 
        arr[z] = new char[s.length()];
        for( y =0 ; y<s.length() ; y++)
        {
            arr[z][y]=s.charAt(y);
        }
    }   
    

    for(z=0;z在
    java
    2D数组最初是
    1D数组的数组
    。这不是
    C++

    • 你只需要知道行数
    • 每个子数组(一行)可以有不同的长度


    请尝试以下代码:

    第一个问题是您没有初始化内部数组。此外,您同时使用了
    nextLine()
    nextLine()
    nextLine()
    方法不考虑您的
    \n
    (换行符)。因此
    nextLine()
    方法将直接使用它,并且不考虑您随后的输入

    publicstaticvoidmain(字符串[]args){
    try(扫描仪输入=新扫描仪(System.in)){
    整数编号;
    do{//这是一些脆弱的代码,但它可以用于此目的
    试一试{
    否=Integer.parseInt(in.nextLine());
    打破
    }捕获(例外e){
    System.out.println(“请只输入一个数值”);
    }
    }虽然(正确);
    System.out.println(“您输入的字符串”+否);
    int z=0,y=0;
    char[][]arr=新字符[no][];
    对于(z=0;z
    您好。我试过了。但显示了相同的错误。您能在计算机上运行代码并粘贴代码吗here@AnwesaRoy使用.nextInt()中的
    读取第一个条目。这不会使用换行符。它将由.nextLine()中的
    使用
    稍后,返回一个空字符串。在使用.nextLine()中的
    读取第一个数字后,在.nextLine()中添加一个额外的
    。还有一个问题。正如Hogenboom先生指出的,由于我们首先使用整数输入,然后是字符串输入,这可能会导致问题,因为nextInt()方法可能不使用“下一行”。这会引起问题吗?好的,我会考虑的。谢谢你的见解。一旦我的声誉达到15,我会赞成你的答案。
    String[][] matrix = new String[3][]; // declare an 2D array with 3 rows and not define column length
    matrix[0] = new String[5]; // 1st line has 5 columns; matrix[0][4] is the last column of 1st row
    matrix[1] = new String[10]; // 2nd line has 10 columns; matrix[1][9] is the last column of 2nd row
    // matrix[2] == null -> true // 3rd line is not initialized
    
    String[][] matrix = new String[2][2]; // declare 2D array and initialize all rows with 1D sub-array with 2 columns (i.e. we have a square).