2013-02-20 39 views
1

在下面的代碼中,調用swapBig(a,some number,somenumber),其中a是數組,它被複制到swapBig()的bleh []中。當bleh []中的值交換時,a中的相應值也會更改。爲什麼會發生這種情況,以及如何編寫代碼以便只修改bleh []而不是原始的a []?非常感謝!爲什麼將數組傳遞給另一個方法會更改原始數組?

public static void swapBig(String bleh[], int to, int from){ //switches data 
    //Actually performing the swaps 
    String temp; 
    temp = bleh[to]; 
    bleh[to] = bleh[from]; 
    bleh[from] = temp; 
} 
public static void quickSort(String a[], String b[], String c[], String d[], 
String e[],String f[], int from, int to){ 
    //performing the quickSort 
    if (from >= to) return; 
    int p = (from + to)/2; 
    int i = from; 
    int j = to; 
    while (i <= j){ 
     if (a[i].compareTo(a[p]) <= 0) 
      i++; 
     else if (a[j].compareTo(a[p]) >= 0) 
      j--; 
     else{ 
      swapBig(a, i, j); 
      swapBig(b, i, j); 
      swapBig(c, i, j); 
      swapBig(d, i, j); 
      swapBig(e, i, j); 
      swapBig(f, i, j); 
      i++; 
      j--; 
     } 
    } 
    if (p<j){ 
     swapBig(a, p, j); 
     swapBig(b, p, j); 
     swapBig(c, p, j); 
     swapBig(d, p, j); 
     swapBig(e, p, j); 
     swapBig(f, p, j); 
     p = j; 
    }else if (p>i){ 
     swapBig(a, p, i); 
     swapBig(b, p, i); 
     swapBig(c, p, i); 
     swapBig(d, p, i); 
     swapBig(e, p, i); 
     swapBig(f, p, i); 
     p = i; 
    } 
    quickSort(a, b, c, d,e,f, from, p-1); 
    quickSort(a, b, c, d,e,f, p + 1, to); 
} 

public static void main (String args []) 
{ 
    //Asking for options (what to sort by/search for) 
    System.out.println("Sort or Search?"); 
    String look = promptFor.nextLine(); 
    if (look.equalsIgnoreCase("Sort")){ 
    System.out.println("Sort by First, Last, Instrument, Instrument Family, 
    Special Title, or University:"); 
    String toSortBy = promptFor.nextLine(); 
    if (toSortBy.equalsIgnoreCase("First")) 
     quickSort(fname,lname,inst,instFam,title,uni,0,9); 
    if (toSortBy.equalsIgnoreCase("Last")) 
     quickSort(lname,fname,inst,instFam,title,uni,0,9); 
    if (toSortBy.equalsIgnoreCase("Instrument")) 
     quickSort(inst,lname,fname,instFam,title,uni,0,9); 
    if (toSortBy.equalsIgnoreCase("Instrument Family")) 
     quickSort(instFam,lname,inst,fname,title,uni,0,9); 
    if (toSortBy.equalsIgnoreCase("Special Title")) 
     quickSort(title,lname,inst,instFam,uni,fname,0,9); 
    if (toSortBy.equalsIgnoreCase("University")) 
     quickSort(uni,lname,inst,instFam,title,fname,0,9); 
    print(); 
    main(null);  } 
    else if (look.equalsIgnoreCase("Search")) { 
    System.out.println("Which last name do you wish to search for?"); 
     searchFor(promptFor.nextLine()); 
    } 
    else 
    { 
     System.out.println("Command Not Recognized\n"); 
     main(null); 
    } 
} 

} 
+1

在Java中,方法參數是引用而不是值,這意味着您將引用傳遞給對象,而不是對象的副本。請注意,基元是按值傳遞的。 – m0skit0 2013-02-20 15:44:44

回答

2

這很簡單。

因爲您將數組傳遞給交換函數,所以數組的值被交換,並且在Java中,參數按引用傳遞。

爲了避免這種情況。

String[] tempArray = a.clone(); 
swapBig(tempArray, i, j); //This will not change the values in a, but tempArray. 
6

您正在傳遞對數組的引用,而不是它的副本。

1

因爲你傳遞的對象與此基準的基準和任何操作將修改

2

可變bleh被複制的主要對象變量a所以實際上bleh指向實際對象的值,如果您有修改bleh這會改變實際的對象,這是因爲java支持按值傳遞。

如果你在通過之前克隆它,你可以得到想要的結果。

1

如果要傳遞數組的副本,請使用arrayVar.clone()方法,System.arraycopy()或Array.copyOf()。

相關問題