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

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

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

        如何通過引用正確傳遞 Integer 類?

        How can I pass an Integer class correctly by reference?(如何通過引用正確傳遞 Integer 類?)
        <legend id='gFApV'><style id='gFApV'><dir id='gFApV'><q id='gFApV'></q></dir></style></legend>

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

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

          • <tfoot id='gFApV'></tfoot>

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

                    <tbody id='gFApV'></tbody>
                  本文介紹了如何通過引用正確傳遞 Integer 類?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我希望有人可以為我澄清這里發生的事情.我在整數類中研究了一會兒,但因為整數 覆蓋 + 運算符我無法弄清楚出了什么問題.我的問題在于這一行:

                  整數 i = 0;我 = 我 + 1;//← 我認為這是在以某種方式創建一個新對象!

                  這是我的推理:我知道 java 是按值傳遞的(或按引用值傳遞),所以我認為在以下示例中,整數對象應每次遞增.

                  公共類 PassByReference {公共靜態整數公司(整數 i){我=我+1;//我認為這一定是**偷偷**創建一個新整數...System.out.println("公司:"+i);返回我;}公共靜態無效主要(字符串[]參數){整數整數 = 新整數(0);for (int i =0; i<10; i++){公司(整數);System.out.println("main:"+integer);}}}

                  這是我的預期輸出:

                  <上一頁>公司:1主要:1公司:2主要:2公司:3主要:3公司:4主要:4公司:5主要:5公司:6主要:6...

                  這是實際輸出.

                  <上一頁>公司:1主要:0公司:1主要:0公司:1主要:0...

                  為什么會這樣?

                  解決方案

                  有兩個問題:

                  1. 整數是按值傳遞,而不是按引用傳遞.更改方法內的引用不會反映到調用方法中傳入的引用中.
                  2. 整數是不可變的.沒有像 Integer#set(i) 這樣的方法.否則,您可以直接使用它.

                  要讓它工作,你需要重新分配 inc() 方法的返回值.

                  integer = inc(integer);

                  <小時>

                  要了解有關按值傳遞的更多信息,這里有另一個示例:

                  public static void main(String...args) {字符串[] 字符串 = 新字符串 [] { "foo", "bar" };更改參考(字符串);System.out.println(Arrays.toString(strings));//仍然是 [foo, bar]更改值(字符串);System.out.println(Arrays.toString(strings));//[foo, foo]}公共靜態無效changeReference(字符串[]字符串){字符串 = 新字符串 [] { "foo", "foo" };}公共靜態無效changeValue(字符串[]字符串){字符串[1] = "foo";}

                  I am hoping that someone can clarify what is happening here for me. I dug around in the integer class for a bit but because integer is overriding the + operator I could not figure out what was going wrong. My problem is with this line:

                  Integer i = 0;
                  i = i + 1;  // ← I think that this is somehow creating a new object!
                  

                  Here is my reasoning: I know that java is pass by value (or pass by value of reference), so I think that in the following example the integer object should be incremented each time.

                  public class PassByReference {
                  
                      public static Integer inc(Integer i) {
                          i = i+1;    // I think that this must be **sneakally** creating a new integer...  
                          System.out.println("Inc: "+i);
                          return i;
                      }
                  
                      public static void main(String[] args) {
                          Integer integer = new Integer(0);
                          for (int i =0; i<10; i++){
                              inc(integer);
                              System.out.println("main: "+integer);
                          }
                      }
                  }
                  

                  This is my expected output:

                  Inc: 1
                  main: 1
                  Inc: 2
                  main: 2
                  Inc: 3
                  main: 3
                  Inc: 4
                  main: 4
                  Inc: 5
                  main: 5
                  Inc: 6
                  main: 6
                  ...
                  

                  This is the actual output.

                  Inc: 1
                  main: 0
                  Inc: 1
                  main: 0
                  Inc: 1
                  main: 0
                  ...
                  

                  Why is it behaving like this?

                  解決方案

                  There are two problems:

                  1. Integer is pass by value, not by reference. Changing the reference inside a method won't be reflected into the passed-in reference in the calling method.
                  2. Integer is immutable. There's no such method like Integer#set(i). You could otherwise just make use of it.

                  To get it to work, you need to reassign the return value of the inc() method.

                  integer = inc(integer);
                  


                  To learn a bit more about passing by value, here's another example:

                  public static void main(String... args) {
                      String[] strings = new String[] { "foo", "bar" };
                      changeReference(strings);
                      System.out.println(Arrays.toString(strings)); // still [foo, bar]
                      changeValue(strings);
                      System.out.println(Arrays.toString(strings)); // [foo, foo]
                  }
                  public static void changeReference(String[] strings) {
                      strings = new String[] { "foo", "foo" };
                  }
                  public static void changeValue(String[] strings) {
                      strings[1] = "foo";
                  }
                  

                  這篇關于如何通過引用正確傳遞 Integer 類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

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

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

                        2. <tfoot id='Ia8o8'></tfoot>
                            <bdo id='Ia8o8'></bdo><ul id='Ia8o8'></ul>
                              <tbody id='Ia8o8'></tbody>

                            主站蜘蛛池模板: 成人av在线播放 | 综合国产在线 | 美女视频一区二区 | 操人网| 亚洲视频一区二区三区 | 福利网址 | 精品久久香蕉国产线看观看亚洲 | 91免费看片神器 | 国产高清视频在线观看 | 在线看av网址 | 午夜影视在线观看 | 中文字幕在线网 | 国产精品乱码一二三区的特点 | 国产高清av免费观看 | 亚洲国产成人av好男人在线观看 | 91视在线国内在线播放酒店 | 精品欧美一区二区精品久久 | av网站观看 | 最新一级毛片 | 波多野结衣一区二区三区在线观看 | 九九热精品免费 | 久久国产高清视频 | 国产精品免费一区二区三区 | 韩三级在线观看 | 国产一区2区 | 午夜丰满寂寞少妇精品 | 无码国模国产在线观看 | 在线91| 国产成人精品一区二区三区 | 日韩成人av在线播放 | 免费视频一区二区 | 在线视频国产一区 | 国产乱肥老妇国产一区二 | 久草网站 | 在线免费黄色 | 国内久久| 伊人免费在线观看高清 | 欧美一区二区网站 | 在线观看深夜视频 | 日本色综合 | 国产一区二区三区久久久久久久久 |