久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  1. <tfoot id='Rx66I'></tfoot><legend id='Rx66I'><style id='Rx66I'><dir id='Rx66I'><q id='Rx66I'></q></dir></style></legend>
      <bdo id='Rx66I'></bdo><ul id='Rx66I'></ul>

    <i id='Rx66I'><tr id='Rx66I'><dt id='Rx66I'><q id='Rx66I'><span id='Rx66I'><b id='Rx66I'><form id='Rx66I'><ins id='Rx66I'></ins><ul id='Rx66I'></ul><sub id='Rx66I'></sub></form><legend id='Rx66I'></legend><bdo id='Rx66I'><pre id='Rx66I'><center id='Rx66I'></center></pre></bdo></b><th id='Rx66I'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Rx66I'><tfoot id='Rx66I'></tfoot><dl id='Rx66I'><fieldset id='Rx66I'></fieldset></dl></div>

      <small id='Rx66I'></small><noframes id='Rx66I'>

      Integer.parseInt(scanner.nextLine()) 與scanner.nextInt()

      Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()(Integer.parseInt(scanner.nextLine()) 與scanner.nextInt())
    1. <small id='9v8MP'></small><noframes id='9v8MP'>

            • <i id='9v8MP'><tr id='9v8MP'><dt id='9v8MP'><q id='9v8MP'><span id='9v8MP'><b id='9v8MP'><form id='9v8MP'><ins id='9v8MP'></ins><ul id='9v8MP'></ul><sub id='9v8MP'></sub></form><legend id='9v8MP'></legend><bdo id='9v8MP'><pre id='9v8MP'><center id='9v8MP'></center></pre></bdo></b><th id='9v8MP'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='9v8MP'><tfoot id='9v8MP'></tfoot><dl id='9v8MP'><fieldset id='9v8MP'></fieldset></dl></div>
                <tbody id='9v8MP'></tbody>
                <bdo id='9v8MP'></bdo><ul id='9v8MP'></ul>
              • <legend id='9v8MP'><style id='9v8MP'><dir id='9v8MP'><q id='9v8MP'></q></dir></style></legend>

                <tfoot id='9v8MP'></tfoot>
                本文介紹了Integer.parseInt(scanner.nextLine()) 與scanner.nextInt()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我的教授傾向于執行以下操作以從用戶那里獲取數字:

                My professor tends to do the following to get a number from the user:

                Scanner scanner = new Scanner(System.in);
                Integer.parseInt(scanner.nextLine());
                

                與簡單地做 scanner.nextInt() 相比有什么好處?

                What are the benefits as opposed to simply doing scanner.nextInt() ?

                java.util.Scanner.java 包含以下內容:

                public int nextInt() {
                    return nextInt(defaultRadix);
                }
                
                public int nextInt(int radix) {
                    // Check cached result
                    if ((typeCache != null) && (typeCache instanceof Integer)
                        && this.radix == radix) {
                        int val = ((Integer)typeCache).intValue();
                        useTypeCache();
                        return val;
                    }
                    setRadix(radix);
                    clearCaches();
                    // Search for next int
                    try {
                        String s = next(integerPattern());
                        if (matcher.group(SIMPLE_GROUP_INDEX) == null)
                            s = processIntegerToken(s);
                        return Integer.parseInt(s, radix);
                    } catch (NumberFormatException nfe) {
                        position = matcher.start(); // don't skip bad token
                        throw new InputMismatchException(nfe.getMessage());
                    }
                }
                

                在我看來,Scanner 本身也調用 Integer.parseInt() ,在額外的惡作劇之上.做簡單的 Integer.parseInt(scanner.nextLine()) 有顯著的性能提升嗎?另一方面有什么缺點嗎?

                As I see it, Scanner calls Integer.parseInt() itself as well, on top of additional hocus pocus. Are there significant performance gains in doing simply Integer.parseInt(scanner.nextLine()) ? Are there on the other hand any drawbacks?

                掃描包含大量數據而不是用戶輸入的文件時會怎樣?

                How about when scanning through a file with significant amount of data, and not a user input?

                推薦答案

                有2個觀察:

                1. 使用 myScannerInstance.nextInt() 會留下一個換行符.因此,如果您在 nextInt() 之后調用 nextLine(),則 nextLine() 將讀取換行符而不是實際數據.因此,您必須在 nextInt() 之后添加另一個 nextLine() 來吞噬那個 dangling 換行符.nextLine() 不會留下換行符.
                1. Using myScannerInstance.nextInt() leaves behind a new line character. So, if you call nextLine() after nextInt(), the nextLine() will read the new line character instead of the actual data. Consequently, you will have to add another nextLine() after the nextInt() to gobble up that dangling new-line character. nextLine() doesn't leave behind a new line character.

                代碼:

                int age=myScannerInstance.nextInt();
                String name = myScannerInstance.nextLine();// here the actual name will not be read. The new line character will be read.
                

                1. nextInt() 將再次返回底層流并讀取.IO 調用需要時間(昂貴).它將進行大量檢查以獲取下一個整數.nextLine() 只會做一次這些檢查.因此,如果您調用一次 nextLine() 并讀取 5 個整數(作為單行字符串),將它們拆分并解析為整數(使用 Integer.parseInt()),這將比單獨讀取每個 int 更快、更高效.
                1. nextInt() will again go back to the underlying stream and read. IO calls take time (expensive). It will do lot of checks to get the next integer. nextLine() will do those checks only once. So, if you call nextLine() once and read 5 integers (as a single line String), split them and parse them as integers (using Integer.parseInt()), it will be faster and more efficient than reading each int individually.

                在運行非常大的循環時,使用 nextLine() + parseInt() 將為您帶來巨大的性能優勢.

                Using nextLine() + parseInt() will give you enormous performance benefit when you are running a very large loop.

                用法:

                使用 nextInt() 給你一個額外的好處,如果輸入文本不是整數,你會得到一個異常.示例 123 被接受.123sdsa 將拋出 InputMismatchException.所以,你可以抓住它并適當地處理它.

                Using nextInt() gives you an additional advantage wherein you will get an exception if the input text is not an integer. example 123 is accepted.. 123sdsa will throw an InputMismatchException. So, you can catch it and handle it appropriately.

                使用 nextLine() 將讀取整行,因此,它將讀取整個字符串 sada1231 然后如果它失敗并返回 NumberFormatException無法將字符串解析為數字.您必須處理該異常.

                Using nextLine() will read the entire line, so, it will read the entire String sada1231 and then fail with NumberFormatException if it cannot parse the String as a number. You will have to handle that exception.

                通常,一個 nextLine()/nextInt() 調用不會有太大的不同.如果你有一個循環或者你正在讀取大量數據,那么使用 readLine()parseInt() 會非常有效.

                Generally, one nextLine() / nextInt() call won't make much of a difference. If you have a loop or if you are reading lot of data, then using readLine() with parseInt() will be very efficient.

                這篇關于Integer.parseInt(scanner.nextLine()) 與scanner.nextInt()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                How to convert Integer to int?(如何將整數轉換為整數?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                Inconsistent behavior on java#39;s ==(java的行為不一致==)
                Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
                  <tbody id='2yvfI'></tbody>

                    <bdo id='2yvfI'></bdo><ul id='2yvfI'></ul>
                    <i id='2yvfI'><tr id='2yvfI'><dt id='2yvfI'><q id='2yvfI'><span id='2yvfI'><b id='2yvfI'><form id='2yvfI'><ins id='2yvfI'></ins><ul id='2yvfI'></ul><sub id='2yvfI'></sub></form><legend id='2yvfI'></legend><bdo id='2yvfI'><pre id='2yvfI'><center id='2yvfI'></center></pre></bdo></b><th id='2yvfI'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='2yvfI'><tfoot id='2yvfI'></tfoot><dl id='2yvfI'><fieldset id='2yvfI'></fieldset></dl></div>

                    1. <tfoot id='2yvfI'></tfoot>
                      • <small id='2yvfI'></small><noframes id='2yvfI'>

                        1. <legend id='2yvfI'><style id='2yvfI'><dir id='2yvfI'><q id='2yvfI'></q></dir></style></legend>

                          主站蜘蛛池模板: 国产精品久久久久久久久动漫 | 国产精品久久久久久久久久免费 | 欧美日韩高清一区 | 在线观看成年视频 | www狠狠干| 亚洲一二三区精品 | 精品一区二区久久 | 久久久久久国产精品免费免费男同 | 啪啪免费 | 91高清在线观看 | 久久亚洲国产 | 产真a观专区 | 国际精品鲁一鲁一区二区小说 | 男女视频在线免费观看 | 欧美日韩成人 | 国产成年人小视频 | 欧美日韩中文字幕在线 | 亚洲一区二区三区免费视频 | 国产一区二区av | 一区二区视频在线 | 亚洲毛片| 欧州一区二区三区 | 国产精品有限公司 | 国产欧美精品一区二区色综合朱莉 | 日日操操操 | 国产在线麻豆精品入口 | 不卡视频一区二区三区 | 美女一级黄 | 久久久黑人 | 自拍偷拍精品 | 天天插天天搞 | 激情久久av一区av二区av三区 | 欧美国产亚洲一区二区 | 成人精品久久 | 一级毛片免费 | 日韩一级免费大片 | 亚洲国产欧美在线人成 | 综合色导航 | 亚洲黄色av | 天天做日日做 | 亚洲一区二区av |