pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  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="meq2cmq" 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="y20g0e2" 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="ye2mcmy" 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>

                          主站蜘蛛池模板: 便携式谷丙转氨酶检测仪|华图生物科技百科 | 广东青藤环境科技有限公司-水质检测 | 深圳成考网-深圳成人高考报名网| 旋片真空泵_真空泵_水环真空泵_真空机组-深圳恒才机电设备有限公司 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 广东佛电电器有限公司|防雷开关|故障电弧断路器|智能量测断路器 广东西屋电气有限公司-广东西屋电气有限公司 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 山东齐鲁漆业有限公司【官网】-工业漆专业生产厂家 | 太空舱_民宿太空舱厂家_移动房屋太空舱价格-豪品建筑 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | 污水处理设备-海普欧环保集团有限公司 | 无水硫酸铝,硫酸铝厂家-淄博双赢新材料科技有限公司 | 茅茅虫AI论文写作助手-免费AIGC论文查重_写毕业论文降重 | 双齿辊破碎机-大型狼牙破碎机视频-对辊破碎机价格/型号图片-金联机械设备生产厂家 | 高速龙门架厂家_监控杆_多功能灯杆_信号灯杆_锂电池太阳能路灯-鑫世源照明 | 上海logo设计 | 商标转让-购买商标专业|放心的商标交易网-蜀易标商标网 | 玻璃钢罐_玻璃钢储罐_盐酸罐厂家-河北华盛节能设备有限公司 | 无缝钢管-聊城无缝钢管-小口径无缝钢管-大口径无缝钢管 - 聊城宽达钢管有限公司 | 山东集装箱活动房|济南集装箱活动房-济南利森集装箱有限公司 | hc22_hc22价格_hc22哈氏合金—东锜特殊钢 | 深圳离婚律师咨询「在线免费」华荣深圳婚姻律师事务所专办离婚纠纷案件 | 抖音短视频运营_企业网站建设_网络推广_全网自媒体营销-东莞市凌天信息科技有限公司 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 合肥抖音SEO网站优化-网站建设-网络推广营销公司-百度爱采购-安徽企匠科技 | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 苏州防水公司_厂房屋面外墙防水_地下室卫生间防水堵漏-苏州伊诺尔防水工程有限公司 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 镀锌方管,无缝方管,伸缩套管,方矩管_山东重鑫致胜金属制品有限公司 | 球盟会·(中国)官方网站| 游泳池设备安装工程_恒温泳池设备_儿童游泳池设备厂家_游泳池水处理设备-东莞市君达泳池设备有限公司 | 云南成人高考网| 回转支承-转盘轴承-回转驱动生产厂家-洛阳隆达轴承有限公司 | 氧化铝球_高铝球_氧化铝研磨球-淄博誉洁陶瓷新材料有限公司 | 手表腕表维修保养鉴定售后服务中心网点 - 名表维修保养 | 热处理炉-退火炉-回火炉设备厂家-丹阳市电炉厂有限公司 | 包装盒厂家_纸盒印刷_礼品盒定制-济南恒印包装有限公司 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 高中学习网-高考生信息学习必备平台 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 |