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

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

    <tfoot id='PfWmb'></tfoot>
  • <small id='PfWmb'></small><noframes id='PfWmb'>

      <bdo id='PfWmb'></bdo><ul id='PfWmb'></ul>

        確定字符串是否是Java中的整數

        Determine if a String is an Integer in Java(確定字符串是否是Java中的整數)

            <bdo id='gIjJ5'></bdo><ul id='gIjJ5'></ul>

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

                    <tbody id='gIjJ5'></tbody>

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

                  本文介紹了確定字符串是否是Java中的整數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試確定字符串數組中的特定項目是否為整數.

                  I'm trying to determine if a particular item in an Array of strings is an integer or not.

                  我正在 .split(" ")'ing 一個 String 形式的中綴表達式,然后嘗試將結果數組拆分為兩個數組;一個用于整數,一個用于運算符,同時丟棄括號和其他雜項.實現這一目標的最佳方法是什么?

                  I am .split(" ")'ing an infix expression in String form, and then trying to split the resultant array into two arrays; one for integers, one for operators, whilst discarding parentheses, and other miscellaneous items. What would be the best way to accomplish this?

                  我以為我可以找到 Integer.isInteger(String arg) 方法之類的,但沒有這樣的運氣.

                  I thought I might be able to find a Integer.isInteger(String arg) method or something, but no such luck.

                  推薦答案

                  最簡單的方法是遍歷 String 并確保所有元素都是給定基數的有效數字.這是盡可能高效的方法,因為您必須至少查看每個元素一次.我想我們可以根據基數對其進行微優化,但就所有意圖和目的而言,這與您所期望的一樣好.

                  The most naive way would be to iterate over the String and make sure all the elements are valid digits for the given radix. This is about as efficient as it could possibly get, since you must look at each element at least once. I suppose we could micro-optimize it based on the radix, but for all intents and purposes this is as good as you can expect to get.

                  public static boolean isInteger(String s) {
                      return isInteger(s,10);
                  }
                  
                  public static boolean isInteger(String s, int radix) {
                      if(s.isEmpty()) return false;
                      for(int i = 0; i < s.length(); i++) {
                          if(i == 0 && s.charAt(i) == '-') {
                              if(s.length() == 1) return false;
                              else continue;
                          }
                          if(Character.digit(s.charAt(i),radix) < 0) return false;
                      }
                      return true;
                  }
                  

                  或者,您可以依靠 Java 庫來實現這一點.它不是基于異常的,并且會捕獲您能想到的幾乎所有錯誤情況.它會貴一點(你必須創建一個 Scanner 對象,在一個非常緊密的循環中你不想這樣做.但它通常不應該太貴,所以對于日常操作應該是相當可靠的.

                  Alternatively, you can rely on the Java library to have this. It's not exception based, and will catch just about every error condition you can think of. It will be a little more expensive (you have to create a Scanner object, which in a critically-tight loop you don't want to do. But it generally shouldn't be too much more expensive, so for day-to-day operations it should be pretty reliable.

                  public static boolean isInteger(String s, int radix) {
                      Scanner sc = new Scanner(s.trim());
                      if(!sc.hasNextInt(radix)) return false;
                      // we know it starts with a valid int, now make sure
                      // there's nothing left!
                      sc.nextInt(radix);
                      return !sc.hasNext();
                  }
                  

                  如果最佳實踐對您來說并不重要,或者您想欺騙負責您的代碼審查的人,請試試這個:

                  If best practices don't matter to you, or you want to troll the guy who does your code reviews, try this on for size:

                  public static boolean isInteger(String s) {
                      try { 
                          Integer.parseInt(s); 
                      } catch(NumberFormatException e) { 
                          return false; 
                      } catch(NullPointerException e) {
                          return false;
                      }
                      // only got here if we didn't return false
                      return true;
                  }
                  

                  這篇關于確定字符串是否是Java中的整數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='McEHh'></tbody>
                  <i id='McEHh'><tr id='McEHh'><dt id='McEHh'><q id='McEHh'><span id='McEHh'><b id='McEHh'><form id='McEHh'><ins id='McEHh'></ins><ul id='McEHh'></ul><sub id='McEHh'></sub></form><legend id='McEHh'></legend><bdo id='McEHh'><pre id='McEHh'><center id='McEHh'></center></pre></bdo></b><th id='McEHh'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='McEHh'><tfoot id='McEHh'></tfoot><dl id='McEHh'><fieldset id='McEHh'></fieldset></dl></div>
                  <tfoot id='McEHh'></tfoot>

                        • <small id='McEHh'></small><noframes id='McEHh'>

                          • <bdo id='McEHh'></bdo><ul id='McEHh'></ul>

                          • <legend id='McEHh'><style id='McEHh'><dir id='McEHh'><q id='McEHh'></q></dir></style></legend>

                            主站蜘蛛池模板: 成人九区 | 久久久久久国产精品 | 免费视频一区 | 久久九九99 | 日韩中文字幕在线不卡 | 中文字幕一区在线观看视频 | 亚洲一区中文字幕在线观看 | 日韩免费网站 | 国产色网站 | 女人精96xxx免费网站p | 日本 欧美 三级 高清 视频 | 久久中文免费视频 | 亚洲精久久久 | 人人人干 | 综合国产 | 日韩综合在线 | 久久99精品视频 | 久久99精品久久 | 国产成人精品一区二 | 日韩欧美在线视频一区 | 亚洲国产看片 | 欧美三级免费观看 | 亚洲黄色av | 天天看逼 | 欧美一区二 | 国产成人精品网站 | 天天夜干| 国精日本亚洲欧州国产中文久久 | 北条麻妃国产九九九精品小说 | 日本电影免费完整观看 | 日韩欧美在线不卡 | 午夜国产一区 | 午夜网站视频 | 99精品久久久国产一区二区三 | 日韩精品专区在线影院重磅 | 国产电影一区二区在线观看 | 国产成人99久久亚洲综合精品 | 天堂资源 | 天天拍天天操 | 成人一区在线观看 | 91免费在线|