第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

用來(lái)自字符串的輸入填充二維數(shù)組的行

用來(lái)自字符串的輸入填充二維數(shù)組的行

慕運(yùn)維8079593 2022-07-27 11:23:41
我有以下問(wèn)題:給定矩陣的每一行的值,列由空格分隔 - 所以我在字符串?dāng)?shù)組中輸入所有行值,刪除空格并將數(shù)字解析為 int 數(shù)組?,F(xiàn)在每一行的值看起來(lái)像 1 個(gè)數(shù)字“12345”,而它們應(yīng)該是“1 2 3 4 5”。如何首先分隔數(shù)字,然后通過(guò)將元素添加到每一行來(lái)填充我的矩陣?謝謝!這是我的代碼:    String n1 = input.nextLine ();    int n = Integer.parseInt(n1); //rows of the matrix    String[] arr = new String [n]; //contains all the rows of the matrix    int [] array = new int [arr.length]; // contains all the elements of the rows of the matrix without whitespace    for (int i = 0; i < arr.length; i++) {        arr [i] = input.nextLine().replaceAll("\\s+","");        array[i] = Integer.parseInt(arr[i]);    }    int matrix [][] = new int [n][arr[0].length()];
查看完整描述

3 回答

?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

您應(yīng)該split()通過(guò)一些字符輸入字符串(在您的示例中為空格)。


示例如何轉(zhuǎn)換String為數(shù)組String(使用split()方法)


// Example input

String input  = "1 2 3 4 5";


// Split elements by space

// So you receive array: {"1", "2", "3", "4", "5"}

String[] numbers = input.split(" ");


for (int position = 0; position < numbers.length; position++) {

    // Get element from "position"

    System.out.println(numbers[position]);

}

示例如何轉(zhuǎn)換String為數(shù)組int


// Example input

String input = "1 2 3 4 5";


// Split elements by space

// So you receive array: {"1", "2", "3", "4", "5"}

String[] strings = input.split(" ");


// Create new array for "ints" (with same size!)

int[] number = new int[strings.length];


// Convert all of the "Strings" to "ints"

for (int position = 0; position < strings.length; position++) {

    number[position] = Integer.parseInt(strings[position]);

}


查看完整回答
反對(duì) 回復(fù) 2022-07-27
?
侃侃無(wú)極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

很難說(shuō),但據(jù)我了解,您正試圖通過(guò)掃描儀逐行輸入矩陣。這可以解決你的問(wèn)題。


    Scanner scanner = new Scanner(System.in);

    //number of rows

    int n = Integer.parseInt(scanner.nextLine());

    int[][] matrix = new int[n][];

    for(int i=0;i<n;i++) {

        String line = scanner.nextLine();

        String[] numbers = line.split(" ");

        matrix[i] = new int[numbers.length];

        for(int j=0;j<numbers.length;j++) {

            matrix[i][j] = Integer.parseInt(numbers[j]);

        }

    }


查看完整回答
反對(duì) 回復(fù) 2022-07-27
?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

在這里你有重要的問(wèn)題:


for (int i = 0; i < arr.length; i++) {

    arr [i] = input.nextLine().replaceAll("\\s+",""); // loses the separator between the number

    array[i] = Integer.parseInt(arr[i]); // makes no sense as you want get all numbers submitted for the current row and no a single one

}

如果您在提交的每一行填充矩陣,則可以使用更少的變量來(lái)進(jìn)行處理。

沒(méi)有經(jīng)過(guò)測(cè)試的代碼,但您應(yīng)該有所了解。


String n1 = input.nextLine();

int n = Integer.parseInt(n1); //rows of the matrix  


int matrix [][] = null; // init it later : as you would have the two dimensions knowledge


for (int i = 0; i < n; i++) {

    String[] numberToken = input.nextLine().split("\\s"); 


    // matrix init : one time

    if (matrix == null){ matrix [][] = new int[n][numberToken.length]; }


    // array of int to contain numbers of the current row

    int[] array = new int[numberToken.length];


    // map String to int. Beware exception  handling that you should do

    for (int j = 0; j < numberToken.length; j++){

        array[j] = Integer.parseInt(numberToken[j]); 

    }

    // populate current row of the matrix

    matrix[i] = array[j];

}


查看完整回答
反對(duì) 回復(fù) 2022-07-27
  • 3 回答
  • 0 關(guān)注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)