Java 我想通过使用另一个带有swap方法的类来交换两个数字,而不是使用通常可用的通用swap函数

Java 我想通过使用另一个带有swap方法的类来交换两个数字,而不是使用通常可用的通用swap函数,java,algorithm,sorting,oop,object,Java,Algorithm,Sorting,Oop,Object,这是冒泡排序算法的Java实现,我使用了另一个带有swap方法的类,当我在swapper类中不使用构造函数,但如果存在构造函数,则根本不交换数组时,这段代码可以正常工作 import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; class swapper { int x, y; void swap() { int temp =

这是冒泡排序算法的Java实现,我使用了另一个带有swap方法的类,当我在swapper类中不使用构造函数,但如果存在构造函数,则根本不交换数组时,这段代码可以正常工作

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

class swapper {
  int x, y;

  void swap() {
    int temp = x;
    x = y;
    y = temp;
  }
}

public class Solution {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] a = new int[n];
    for (int a_i = 0; a_i < n; a_i++) {
      a[a_i] = in.nextInt();
    }
    int swaps = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i - 1; j++) {
        if (a[j] > a[j + 1]) {
          swaps++;
          swapper s = new swapper();
          s.x = a[j];
          s.y = a[j + 1];
          a[j] = s.y;
          a[j + 1] = s.x;
        }
      }
    }
    System.out.println(
        "Array is sorted in "
            + swaps
            + " swaps.\nFirst Element: "
            + a[0]
            + "\nLast Element: "
            + a[n - 1]);
  }
}
import java.io.*;
导入java.math.*;
导入java.text.*;
导入java.util.*;
导入java.util.regex.*;
类交换程序{
int x,y;
无效掉期(){
int-temp=x;
x=y;
y=温度;
}
}
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int n=in.nextInt();
int[]a=新的int[n];
for(int a_i=0;a_ia[j+1]){
交换++;
交换程序s=新交换程序();
s、 x=a[j];
s、 y=a[j+1];
a[j]=s.y;
a[j+1]=s.x;
}
}
}
System.out.println(
“数组已按排序”
+互换
+“交换。\n第一个元素:”
+a[0]
+“\n上一个元素:”
+a[n-1]);
}
}
但是,当我使用构造函数为我的“s”对象的x和y赋值时,这段代码根本不交换任何元素

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

class swapper {
  int x, y;

  swapper(int a, int b) {
    x = a;
    y = b;
  }

  void swap() {
    int temp = x;
    x = y;
    y = temp;
  }
}

public class Solution {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] a = new int[n];
    for (int a_i = 0; a_i < n; a_i++) {
      a[a_i] = in.nextInt();
    }
    int swaps = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i - 1; j++) {
        if (a[j] > a[j + 1]) {
          swaps++;
          swapper s = new swapper(a[j], a[j + 1]);
          s.swap();
          a[j] = s.y;
          a[j + 1] = s.x;
        }
      }
    }
    System.out.println(
        "Array is sorted in "
            + swaps
            + " swaps.\nFirst Element: "
            + a[0]
            + "\nLast Element: "
            + a[n - 1]);
  }
}
import java.io.*;
导入java.math.*;
导入java.text.*;
导入java.util.*;
导入java.util.regex.*;
类交换程序{
int x,y;
交换程序(内部a、内部b){
x=a;
y=b;
}
无效掉期(){
int-temp=x;
x=y;
y=温度;
}
}
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int n=in.nextInt();
int[]a=新的int[n];
for(int a_i=0;a_ia[j+1]){
交换++;
交换程序s=新的交换程序(a[j],a[j+1]);
s、 交换();
a[j]=s.y;
a[j+1]=s.x;
}
}
}
System.out.println(
“数组已按排序”
+互换
+“交换。\n第一个元素:”
+a[0]
+“\n上一个元素:”
+a[n-1]);
}
}
这两种代码的唯一区别是存在一个构造函数来为实例变量赋值


第一个代码使用手动赋值,而第二个代码使用构造函数。

基本上是两次交换两个数字,因此根本不交换它们:

swapper s = new swapper(a[j],a[j+1]); // this assigns a[j] to s.x and a[j+1] to s.y
s.swap(); // this swaps s.x and s.y
a[j] = s.y; // this assigns the original value of s.x (a[j]) to a[j]
a[j+1] = s.x; // this assigns the original value of s.y (a[j+1]) to a[j+1]
要使交换按预期工作,请将其更改为:

swapper s = new swapper(a[j],a[j+1]); 
s.swap();
a[j] = s.x;
a[j+1] = s.y;

实际上,您将两个数字交换两次,因此根本不交换它们:

swapper s = new swapper(a[j],a[j+1]); // this assigns a[j] to s.x and a[j+1] to s.y
s.swap(); // this swaps s.x and s.y
a[j] = s.y; // this assigns the original value of s.x (a[j]) to a[j]
a[j+1] = s.x; // this assigns the original value of s.y (a[j+1]) to a[j+1]
要使交换按预期工作,请将其更改为:

swapper s = new swapper(a[j],a[j+1]); 
s.swap();
a[j] = s.x;
a[j+1] = s.y;

太多的代码,太没有格式化。请提供我已经在Java中查找了引用,但无法找出任何内容,非常感谢您的帮助。请不要像在第一个代码中那样调用
swap
方法。。。或者不要交换作业。太多的代码,太没有格式化。请提供我已经在Java中查找了引用,但无法找出任何内容,非常感谢您的帮助。请不要像在第一个代码中那样调用
swap
方法。。。或者不要交换任务。