为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕桂英4014372 2023-06-21 16:19:13
我正在编写一个代码,其中将从用户处获取输入并将其存储在二维动态数组中。我们知道没有。数组的行数。但数组的列数是动态的。输入的形式如下所示: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();         }}
查看完整描述

3 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

您的代码有 3 个问题:

  1. 您永远不会初始化内部数组。用 来做arr[z] = new char[s.length()];

  2. 你定义的方式arrOfStr。您用空白子字符串分割字符串。相反,只需像这样 s使用:charAtarr[z][y] = s.charAt(y);

  3. 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);

    }

}   


查看完整回答
反对 回复 2023-06-21
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

试试这个代码:


第一个问题是您没有初始化内部数组。此外,您同时使用了nextInt()和nextLine()。该nextInt()方法不考虑您的 \n(newLine symbol) 。所以该nextLine()方法会直接消费它,不会考虑你后续的输入。


public static void main(String[] args) {

        try (Scanner in = new Scanner(System.in)) {

            Integer no;

            do { // this is some flaky code but it works for this purpose

                try {

                    no = Integer.parseInt(in.nextLine());

                    break;

                } catch (Exception e) {

                    System.out.println("please enter only a numeric value");

                }

            } while (true);


            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("");

                arr[z] = new char[arrOfStr.length];

                for (y = 0; y < arrOfStr.length; y++) {

                    System.out.println();

                    arr[z][y] = arrOfStr[y].charAt(0);

                }

            }

            for (char[] charArr : arr) {

                for (char c : charArr) {

                    System.out.println(c);

                }

            }

        }

    }


查看完整回答
反对 回复 2023-06-21
?
慕仙森

TA贡献1827条经验 获得超7个赞

java二维数组中最初是1D array of arrays. 这不是C++

  • 您只需知道行数即可;

  • 每个子数组(一行)可以有不同的长度。

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).



查看完整回答
反对 回复 2023-06-21
  • 3 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信