2014-01-23 29 views
0

當我運行這段代碼,它打印0,1,2,但我不知道爲什麼。你能解釋我嗎?陣列和錯誤的印刷

public void run() { 
     int[] arr = new int [3]; 
     for(int i=0; i<arr.length;i++){ 
      arr[i]=i; 
     } 
     domore(arr); 
     for(int i=0; i<arr.length;i++){ 
      println(arr[i]); 
     } 
    } 

    private void domore(int[] arr) { 
     // TODO Auto-generated method stub 
     int [] att = new int [3]; 
     for(int i=0; i<att.length;i++){ 
      att[i]=77; 
     } 
     arr=att; 
    } 
+0

使用'arr = att;'您只是將引用的本地副本重新分配給數組。 Google傳值與傳遞引用。 – blgt

回答

0

你逝去的任何參數值功能,該範圍將是本地的只是功能。

而另一件事是,在java中您呼叫的價值沒有被引用的函數。

如果你想使用修改後的值,你從函數必須return這一點。

public void run() { 
      int[] arr = new int [3]; 
      for(int i=0; i<arr.length;i++){ 
       arr[i]=i; 
      } 
      // arr is in run() method 
      domore(arr); 
      for(int i=0; i<arr.length;i++){ 
       System.out.println(arr[i]); 
      } 
     } 

     private void domore(int[] arr) { 
      // TODO Auto-generated method stub 
      int [] att = new int [3]; 
      for(int i=0; i<att.length;i++){ 
       att[i]=77; 
      } 
      arr=att; // here scope of arr is local for domore() method only 
      // so arr in run() method will not modified. 
     } 
8

在Java中 - 「對對象的引用通過值傳遞」。您在doMore()方法中所做的任何更改都不會反映到原始數組中,因爲您正在重新分配所傳遞的引用......(請使用camelCase命名方法/字段,即使用doMore()而不是domore)。

public void run() { 
     int[] arr = new int [3]; // arr points to an integer array of size 3 
     doMore(arr); 
     } 
private static void doMore(int[] arrNew) { // arrNew is copy of arr so it also points to the same integer array of size 3. So, any changes made by arrNew are as good as changes made by arr (and yes data of arr is changed..)   
     // TODO Auto-generated method stub 
     int [] arrNew = new int [3];   // you are making arrNew point to new integer array of size 3. So, now arrNew points to a new object and not to the one pointed by arr. 
     for(int i=0; i<arrNew.length;i++){ 
      arrNew[i]=77; 
     } 

    } 
+0

但如果我用「doMore」方法編寫arr [1] = 33;它會改變! – user3151874

+0

@ user3151874這是因爲通過了引用的副本。這意味着對作爲參數傳遞的對象所做的更改將影響初始引用。但是,爲該引用分配新的引用不會影響傳入該方法的引用。請參閱:http://www.javaranch.com/campfire/StoryPassBy.jsp –

+0

@ user3151874 - 檢查我編輯的答案.. – TheLostMind

3

在該方法domoreint[] arr作爲參數被傳遞,這意味着參考的副本被傳遞(又名由值)。在該方法中,創建一個新的int[],然後將其分配給參考副本(int[] arr)。這絕不會改變在run方法中創建的初始int[]。它只會將傳遞的參數分配給domore()中新創建的數組。

1

因爲你把你的arr - 陣列的參考方法domore。在它裏面,你可以通過設置arr=att來減少這個參考。外方法run是無法識別這個新設置的基準,並會留在舊的數組引用,你已經把值0, 1, 2

0

domore是一個void函數。如果你不想改變你的價值觀,你將不得不返還一些東西。

private int[] domore (int[] arr){ 
int [] att = new int [3]; 
    for(int i=0; i<att.length;i++){ 
     att[i]=77; 
    } 
    return att; 
} 

你叫它是:

arr = domore(arr); 
+0

您只需在方法參數「arr」中設置值。因爲外部方法有這個參考! – bobbel

1

嘗試在domode方法返回數組ARR

public void run() { 
    int[] arr = new int [3]; 
    for(int i=0; i<arr.length;i++){ 
     arr[i]=i; 
    } 
    arr = domore(arr); 
    for(int i=0; i<arr.length;i++){ 
     println(arr[i]); 
    } 
} 

private int[] domore(int[] arr) { 
    // TODO Auto-generated method stub 
    int [] att = new int [3]; 
    for(int i=0; i<att.length;i++){ 
     att[i]=77; 
    } 
    return(att); 
} 
+0

有寫無效,你返回int [] – user3151874

+0

是的,你是對的,我糾正它。 –

0

您與原語的數組工作。如果您將int更改爲Integer(從基元到對象),您不應該有這個問題。

int a=5; 
int b=a; 
b=10; 

- >一仍將5. 如果你想使用原語,然後就返回int數組,並將其分配到初始陣列

arr=doMore(arr); 
-1

使用 ARR =(INT [ ])att.clone();或者你也可以使用Array.copyOf方法!

代替ARR = ATT

+0

這並不能解決問題,因爲引用仍然會保存帶有元素'0,1,2'的數組! – bobbel

+0

@bobble ......我以爲他想改變數組的內容。 –

+0

這是真的!但是......如果你用'domore'方法克隆數組,這個問題沒有什麼好處。在'run'方法中,'arr'變量仍然保持對包含'0,1,2'的數組的引用。 – bobbel