简体   繁体   中英

Why does passing an array to another method change the original array?

In the following code, calling swapBig(a,some number,somenumber), where a is an array, is copied to bleh[] in swapBig(). When values in bleh[] are swapped, the corresponding values in a[] are also changed. Why does this happen, and how would I write the code so that only bleh[] is modified and not the original a[]? Thanks so much!

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);
    }
}

}

您正在传递对数组的引用,而不是它的副本。

Variable bleh is copied the value of variable a so actually bleh is pointing to actual object and if you modify with bleh this will change actual object this is because of java supports pass by value.

You can get the desired result if you clone it before pass.

It's simple enough.

The values of the arrays are swapped, because you passed the array to the swapping function, and in Java, parameters are passed by reference.

To avoid this.

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

因为你已经传递了对象的引用,任何使用这个引用的操作都会修改主对象

如果要传递数组的副本,请使用 arrayVar.clone() 方法、System.arraycopy() 或 Array.copyOf()。

When you pass an array to a method, java does not create a duplicate of the array X to that method. It passes the reference to array X.

Example:

public void swap(Color a, Color b) {
    Color temp = a;
    a = b;
    b = temp;
}

This has no effect. But,

.
.
.
   swap(x, y);
}

public void swap(Color[] a, Color[] b) {
   Color temp = a[0];
   a[0] = b[0];
   b[0] = temp;
}

this will swap the 1st value of x[] with the first value of y[].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM