-4

In Java why pass in a parameter, the parameter in a method to get the right change, but after calling this method in the main method of the parameter is not changed?

My code:

public void RSort123(Student[] stu) {
    int k = 0;
    Student[] stu1 = new Student[this.len];
    for (int i = 0; i < this.len; i++)
        stu1[i] = new Student();
    Clear(array);
    for (int i = 0; i <= 100; i++) {
        for (int j = 0; j < this.len; j++) {
            if (stu[j].english == i) {
                array[i][k++] = j;

            }
        }
        k = 0;
    }// 分配

    for (int i = 100, j = 0; i >= 0; i--) {
        for (int l = 0; array[i][l] != -1; l++) {
            stu1[j] = stu[array[i][l]];
            j++;
        }
    }// 收集

    stu = stu1;
    System.out.println("---------------stu----------------");
    for (int p = 0; p < len; p++)
        stu[p].display();
    System.out.println("---------------stu1---------------");
    for (int p = 0; p < len; p++)
        stu1[p].display();
    System.out.println("---------------end----------------");
}

Code Part of the main code

//Call the method.  But not work, But in RSort123() method stu changed. Why?
rs.RSort123(stu);            
System.out.println("姓名\t语文\t数学\t英语\t总分");
for(int i=0; i<length; i++){
    stu[i].display();
}
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
wxisme
  • 11
  • 3
  • Where is "length" defined? Does the function enter the loop at all? – Andreas L. Nov 11 '14 at 14:44
  • Sorry sport, I had a fairly comprehensive answer for you, but just as I wanted to add it, the question was marked as a duplicate. Your problem lies in the statement `stu = stu1`. That simply won't work, because it makes no change to the referenced object. Try passing `stu1` into the method as a parameter rather than creating it inside the method. Your code will definitely change the values in `stu` that way... maybe it will even work as expected. :-) – Achim Schmitz Nov 11 '14 at 17:02

2 Answers2

1

Java passes parameters by value, so while you can make changes to what is in stu, you cannot replace stu itself.

Think of stu as a local variable which refers to the same (jn this case, Array) as the stu in main, so that changing stu in the method does not affect the stuin main (but changing what is in the Array they share will show up in both).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Sorry but that's not quite true. Parameters in Java are passed by reference, not by value. If that weren't the case, you couldn't change the values in an array by passing it to a method at all. I think what you mean is that you cannot change the reference within the method, assigning the parameter ref to another object within the method doesn't alter the object given as a parameter. – Achim Schmitz Nov 11 '14 at 14:57
  • @AchimSchmitz: In most languages, pass by reference means that the assignment made in the original code (`stu = stu`) would have worked. Maybe Java uses the term differently, but the point is the same. – Scott Hunter Nov 11 '14 at 15:12
  • why are people downvoting this question. i think it's not a stupid question and a fault many beginners will do, even if you tell them not to. – momor10 Nov 11 '14 at 15:40
  • @momosxp: The (too?) charitable answer would be that it is a duplicate. – Scott Hunter Nov 11 '14 at 15:50
  • I've had a think about the "duplicate" matter and it makes very little sense to me. The poor fellow who asked the question didn't know in advance that he was handling the parameter reference incorrectly. It would have been sufficient to point out the line of code in error and point him to that other answer... assuming you knew about it. To just go voting the question down is, to put it mildly, arrogant. – Achim Schmitz Nov 11 '14 at 16:47
  • I am sorry ,I am a beginners . I have another question: Why I call the 'fs.Sort123(stu, length);' It is worked,But the 'rs.RSort123(stu,length);' still dont work. The former is Bubble sort,and the The latter is Radix sort,Both are according to the priority order of each subject result.I really is not understanding on the problem. Although I have tried my best. – wxisme Nov 12 '14 at 14:06
  • @wxisme: Not knowing what `fs` or `rs` are, or in what way(s) things "still don't work", I don't see how anyone could answer. – Scott Hunter Nov 12 '14 at 14:18
  • I finally understand, thoroughly absolutely won't make the mistake again, but still want to thank every person who answered my question. – wxisme Nov 12 '14 at 14:23
  • @Scott Hunter My English is not very good, I'm sorry – wxisme Nov 12 '14 at 14:38
  • @wxisme: You could post your new question AS a new question, so that you could provide the needed info (and link back to this one, if you think it would help). – Scott Hunter Nov 12 '14 at 14:44
  • I've learned Thank you – wxisme Nov 12 '14 at 14:53
1

the fault is that you modify another object stu1 and then change the reference of stu to stu1. That will work in your method but won't effect the reference of stu in your main method. it will still refere to your old stu object. so you have to do it like that:

public Student[] RSort123(Student[] stu){

        int k = 0;
        Student[] stu1 = new Student[this.len];
        for(int i=0; i<this.len; i++)
            stu1[i] = new Student();
        Clear(array);
        for(int i=0; i<=100; i++){
            for(int j=0; j<this.len; j++){
                if(stu[j].english == i){
                    array[i][k++] = j;

                }
            }
            k = 0;
        }//分配


        for(int i=100,j=0; i>=0; i--){
            for(int l=0; array[i][l]!=-1; l++){
                stu1[j] = stu[array[i][l]];
                j++;
            }
        }//收集

        stu = stu1;
        System.out.println("---------------stu----------------");
        for(int p=0; p<len; p++)
            stu[p].display();
        System.out.println("---------------stu1---------------");
        for(int p=0; p<len; p++)
            stu1[p].display();
        System.out.println("---------------end----------------");
        return stu;
    }

and in your main:

stu=rs.RSort123(stu);            **//Call the method.  But not work, But in RSort123()method stu changed.  Why?**
System.out.println("姓名\t语文\t数学\t英语\t总分");
for(int i=0; i<length; i++){
    stu[i].display();
}

you have to let your method return the modified object stu and then in the main method set your stu object to the new object returned by your method.

momor10
  • 478
  • 3
  • 11
  • So if the code had made modifications directly to the contents of `stu`, that would not have worked? – Scott Hunter Nov 11 '14 at 14:54
  • no, you're right of course in your answer. my 2 cents of code will work but the explanation isn't right. see here: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – momor10 Nov 11 '14 at 14:59