亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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條經驗 獲得超8個贊

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 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號