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

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

输入的形式如下所示:

3

啊啊啊啊啊

ghgh

伊蒂胡吉

由于首先输入 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();     

    }

}


慕桂英4014372
浏览 141回答 3
3回答

喵喵时光机

您的代码有 3 个问题:您永远不会初始化内部数组。用 来做arr[z] = new char[s.length()];。你定义的方式arrOfStr。您用空白子字符串分割字符串。相反,只需像这样&nbsp;s使用:charAtarr[z][y] = s.charAt(y);nextInt正如评论所建议的那样,存在未考虑(输入)字符的问题\n。所以使用int no=Integer.parseInt(in.nextLine());,而不是使用nextInt。最终代码应如下所示:for(z=0 ; z<no ; z++){&nbsp; &nbsp; String s = in.nextLine();&nbsp;&nbsp; &nbsp; arr[z] = new char[s.length()];&nbsp; &nbsp; for( y =0 ; y<s.length() ; y++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; arr[z][y]=s.charAt(y);&nbsp; &nbsp; }}&nbsp; &nbsp;

眼眸繁星

试试这个代码:第一个问题是您没有初始化内部数组。此外,您同时使用了nextInt()和nextLine()。该nextInt()方法不考虑您的 \n(newLine symbol) 。所以该nextLine()方法会直接消费它,不会考虑你后续的输入。public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; try (Scanner in = new Scanner(System.in)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer no;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do { // this is some flaky code but it works for this purpose&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no = Integer.parseInt(in.nextLine());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("please enter only a numeric value");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You entered string " + no);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int z = 0, y = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char[][] arr = new char[no][];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (z = 0; z < no; z++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String s = in.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] arrOfStr = s.split("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[z] = new char[arrOfStr.length];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (y = 0; y < arrOfStr.length; y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[z][y] = arrOfStr[y].charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (char[] charArr : arr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (char c : charArr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕仙森

在java二维数组中最初是1D array of arrays.&nbsp;这不是C++:您只需知道行数即可;每个子数组(一行)可以有不同的长度。String[][] matrix = new String[3][]; // declare an 2D array with 3 rows and not define column lengthmatrix[0] = new String[5]; // 1st line has 5 columns; matrix[0][4] is the last column of 1st rowmatrix[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 initializedString[][] 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).
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java