java - 为什么将数组传递给另一个方法会更改原始数组?

标签 java arrays

在下面的代码中,调用 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);
    }
}

}

最佳答案

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

关于java - 为什么将数组传递给另一个方法会更改原始数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14983953/

相关文章:

java - 防止 Java 代理在运行时附加

java - 为什么我会收到错误 : cannot find symbol variable in R class

java - System.setProperty 和 System.getProperty

javascript - 正确更新/合并 React Redux 存储中的数组值,不重复

python - 使 3-d numpy 数组的每个第 n 个切片连续

java - 编译出现问题: Symbol error in csv file split in java

Java 发布和调试属性文件

java - 将语言名称转换为 ISO 639 语言代码

javascript - 在 map 功能中传递 Prop 不起作用。将其作为 key 传递

php - 缓冲递归 foreach 循环输出的函数式编程替代方案