如何在附带的”StudentTest.java”文件中交换对象s1和s3。

huangapple go评论97阅读模式
英文:

How to swap object s1 and s3 in attched StudentTest.java file

问题

问题是交换函数应该在main()函数中交换引用,但这并不是这样的情况,原因是Java中参数传递的方式。

在Java中,参数传递是按值传递的,这意味着当你将对象传递给一个方法时,实际上传递的是对象的引用的副本,而不是对象本身。在swap_s1_n_s3方法中,虽然你交换了students数组中两个元素的位置,但是你没有改变main()方法中students数组中的引用。所以,虽然在swap_s1_n_s3方法内部students数组中的元素引用发生了交换,但在main()方法中,s1s3仍然分别引用原始的Student对象。

简而言之,Java中的参数传递方式导致在swap_s1_n_s3方法内部的交换不会影响main()方法中的引用。如果你想要在main()方法中交换引用,你可以使用一个包装对象或者返回一个新的数组,以便在main()方法中得到交换后的数组。

英文:

Below is a StudentTest class which uses Student.java to create student

Student class

class Student
 {
    int id;
    static int computeCount;

    int age;
    String name;
    String gender;
    short rank;
    double gpa;
    long phone;
    char degree;

    boolean international;
    double tutionfees=12000;
    double internationalFees=5000;

    Student(int id,String name,String gender,int age,long phone,double gpa, char degree){
      // id=newId;
      // name=newName;
      // gender=newGender;
      // age=newAge;
      // phone=newPhone;
      // gpa=newGpa;
      // degree=newDegree;
      this(id,name,gender, age, phone,gpa,degree,false);    // delegate to second parameterized constructor 
      //international=isInternational;
    }


    // Second parameterized constructor
    Student(int id,String name,String gender,int age,long phone,double gpa, char degree,boolean international){
      this.id=id;
      this.name=name;
      this.gender=gender;
      this.age=age;
      this.phone=phone;
      this.gpa=gpa;
      this.degree=degree;
      this.international=international;
}

StudentTest class

class StudentTest {
	 public static void main(String args[])
     {
        Student s1=new Student(1000,"John","Male",18,2234567890L,3.8,'B');
        Student s2=new Student(1001,"Indresh","Male",20,2234567893L,4.0,'C',true);
        Student s3 = new Student(1002,"Anita","Female",25,2234568793L,4.2,'B',true);
        System.out.println("student1: " + s1.name);
        System.out.println("student2: " + s2.name);
        System.out.println("student3: " + s3.name);

        Student[] students={s1,s2,s3};    // object array of Student type

        // Swap s1 and s3 references

        // Before Swap
        System.out.println("\n\nBefore Swap\n--------");
        System.out.println("student1: " + s1.name);
        System.out.println("student2: " + s2.name);
        System.out.println("student3: " + s3.name);

         // After swap

        swap_s1_n_s3(students,0,2);

        System.out.println("\n\nAfter Swap, Inside StudentTest class\n--------");

        System.out.println("\n\nstudent1: " + s1.name);
        System.out.println("student2: " + s2.name);
        System.out.println("student3: " + s3.name);

   }


static void swap_s1_n_s3(Student[] students,int first,int last)
   {    System.out.println("\n\n------------\nINSIDE SWAP METHOD");
        Student s5;
        s5=students[first];
        students[first]=students[last];
        students[last]=s5;
        
          System.out.println("\n\nstudent1: " + students[0].name);
        System.out.println("student2: " + students[1].name);
        System.out.println("student3: " + students[2].name);
        System.out.println("\nEXIT SWAP METHOD");

   }
 }  

OUTPUT

$ java StudentTest

student1: Joan
student2: Indresh
student3: Anita

Before Swap

student1: John
student2: Indresh
student3: Anita


INSIDE SWAP METHOD

student1: Anita
student2: Indresh
student3: John

EXIT SWAP METHOD

After Swap, Inside StudentTest class

student1: John
student2: Indresh
student3: Anita

My question is the swap funtion should also swap the references in main(), but this is not the case, why ?

答案1

得分: 2

交换逻辑正常工作。问题属于不同的性质。

在所呈现的代码中,变量s1s2s3用于保存初始的Student对象,初始化students数组并用于调试打印。然而,无论对students做了什么操作,这三个变量始终引用最初分配给它们的同一个Student对象。当变量声明为final时,这一点变得明显。

在调试打印Student对象时,应该通过students数组进行访问:

System.out.println("student1: " + students[0].name);
System.out.println("student2: " + students[1].name);
System.out.println("student3: " + students[2].name);

Ideone演示

英文:

The swap-logic works correctly. The problem is of a different nature.

In the code presented, the variables s1, s2 and s3 are used to hold the initial Student-objects, initialize the students-array and printed as debugging. No matter, however, what is done to and with students, these three variables reference the same Student-object that was originally assigned to them. This gets obvious when the variables are declared as final.

When printing the Students for debugging, they should be accessed through the students-array:

System.out.println("student1: " + students[0].name);
System.out.println("student2: " + students[1].name);
System.out.println("student3: " + students[2].name);

<kbd>Ideone demo</kbd>

答案2

得分: 1

将主方法替换为以下内容,你会有一个想法。

不要通过引用访问元素,而是通过array[index]进行访问,你会得到你想要的确切结果。

public static void main(String args[]) {
    Student s1 = new Student(1000, "John", "Male", 18, 2234567890L, 3.8, 'B');
    Student s2 = new Student(1001, "Indresh", "Male", 20, 2234567893L, 4.0, 'C', true);
    Student s3 = new Student(1002, "Anita", "Female", 25, 2234568793L, 4.2, 'B', true);
    System.out.println("student1: " + s1.name);
    System.out.println("student2: " + s2.name);
    System.out.println("student3: " + s3.name);

    Student[] students = {s1, s2, s3};    // Student类型的对象数组

    // 交换s1和s3的引用

    // 交换之前
    System.out.println("\n\n交换之前\n--------");
    System.out.println("student1: " + s1.name);
    System.out.println("student2: " + s2.name);
    System.out.println("student3: " + s3.name);

    // 交换后

    swap_s1_n_s3(students, 0, 2);

    System.out.println("\n\n交换后,在StudentTest类内部\n--------");

    System.out.println("student1: " + students[0].name);
    System.out.println("student2: " + students[1].name);
    System.out.println("student3: " + students[2].name);

}
英文:

Replace main method as below, you will get an idea.

Instead of accessing element by reference you need to access by array[index], you will get exact result what you want.

 public static void main(String args[]) {
Student s1 = new Student(1000, &quot;John&quot;, &quot;Male&quot;, 18, 2234567890L, 3.8, &#39;B&#39;);
Student s2 = new Student(1001, &quot;Indresh&quot;, &quot;Male&quot;, 20, 2234567893L, 4.0, &#39;C&#39;, true);
Student s3 = new Student(1002, &quot;Anita&quot;, &quot;Female&quot;, 25, 2234568793L, 4.2, &#39;B&#39;, true);
System.out.println(&quot;student1: &quot; + s1.name);
System.out.println(&quot;student2: &quot; + s2.name);
System.out.println(&quot;student3: &quot; + s3.name);
Student[] students = {s1, s2, s3};    // object array of Student type
// Swap s1 and s3 references
// Before Swap
System.out.println(&quot;\n\nBefore Swap\n--------&quot;);
System.out.println(&quot;student1: &quot; + s1.name);
System.out.println(&quot;student2: &quot; + s2.name);
System.out.println(&quot;student3: &quot; + s3.name);
// After swap
swap_s1_n_s3(students, 0, 2);
System.out.println(&quot;\n\nAfter Swap, Inside StudentTest class\n--------&quot;);
System.out.println(&quot;student1: &quot; + students[0].name);
System.out.println(&quot;student2: &quot; +  students[1].name);
System.out.println(&quot;student3: &quot; +  students[2].name);
}

huangapple
  • 本文由 发表于 2020年8月17日 04:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63441446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定