Java 排序数组类不工作

Java 排序数组类不工作,java,arrays,class,sorting,methods,Java,Arrays,Class,Sorting,Methods,我编写了一个类,应该创建一个随机数组,并对其进行排序。我为每个方法编写了方法,每个方法中的代码都独立工作。但是当我将它们一起使用时,我无法使用sort方法对随机方法创建的内容进行排序。有什么想法吗 注意,如果有帮助的话,我正在用Java编写代码并使用NetBeans。我不想要更简单的编码方法,我只是想要帮助它工作 这是我的密码 public class SortUtility { private int[] num; static Random rand = new Rando

我编写了一个类,应该创建一个随机数组,并对其进行排序。我为每个方法编写了方法,每个方法中的代码都独立工作。但是当我将它们一起使用时,我无法使用sort方法对随机方法创建的内容进行排序。有什么想法吗

注意,如果有帮助的话,我正在用Java编写代码并使用NetBeans。我不想要更简单的编码方法,我只是想要帮助它工作

这是我的密码

public class SortUtility {

    private int[] num;
    static Random rand = new Random();
    private int c = 0;
    private int swap = 0;
    private int compare = 0;
    private int p = 10;
    private int[] b;
    //private int[] ex;

    public SortUtility() {
        num = new int[p];

    }

    public SortUtility(int[] Startnum) {
        int[] b = Startnum;
    }

    public int[] createRandomArray(int max, int min, int[] num) {
        //private int [10] ex;
        int [] ex = new int[num.length];

        for (int i = 0; i < num.length; i++) {
            num[i] = rand.nextInt(max + 1 - min) + min;
        }

        for (int i = 0; i < num.length; i++) {
            ex[i] = num[i];
        }

        return ex;
    }

    public int[] sortArray1(int[] ex) {
        int a;
        printItems();

        do {
            c++;
            for (a = 0; a < num.length - 1; a++) {
                compare++;
                if (num[a] > num[a + 1]) {
                    int temp = num[a];
                    num[a] = num[a + 1];
                    num[a + 1] = temp;
                    swap++;
                    System.out.println(num[2]);
                }
            }
            printItems();
        } while (c != num.length);

        return num;
    }

    public void printItems() {
        System.out.print("\nPass " + c + " Compares " + compare + " Swaps " + swap + " " + "items ");
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i] + " ");
        }
    }

    public int[] sortArray2(int[] ex) {
        int a;
        int swap2 = 0;
        printItems();

        do {
            c++;
            for (a = 0; a < num.length - 1 - (c - 1); a++) {
                compare++;
                if (num[a] > num[a + 1]) {
                    int temp = num[a];
                    num[a] = num[a + 1];
                    num[a + 1] = temp;
                    swap++;
                }
            }
            if (swap2 < swap && swap2 != swap) {
                if (swap2 < swap) {
                    swap2 = swap;
                }
            }
            swap2++;
            printItems();
        } while (swap + 2 != swap2);

        return num;
    }
}

这就证明了随机数组方法是有效的,排序也是有效的。你写的代码是错误的。首先是变化

     public SortUtility(int[] Startnum) {
        int[] b = Startnum;
    }

to:
    public SortUtility(int[] Startnum) {
    num = Startnum;
    }

and then
将createRandomArray的方法签名更改为:

    public int[] createRandomArray(int max, int min) 
createRandomArray中的更改是必需的,因为您正在填充不同对象中的数组,并尝试对不同的数组对象进行排序。 构造函数中的更改是有保证的,因为您为构造函数本地对象分配了数组空间,这是无用的

请使用以下修改后的代码作为参考

     import java.util.Random;


public class SortClient {

    public static void main(String[] args) {
        int[] num = {25, 7, 99, 14, 55, 3, 47, 6, 1};
        SortUtility ds = new SortUtility();
        //SortUtility j = new SortUtility(l);
        SortUtility ab = new SortUtility(num);
        int[] a = ab.createRandomArray(99, 0);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        ab.sortArray1(a);




    }
}

 class SortUtility {

private int[] num;
static Random rand = new Random();
private int c = 0;
private int swap = 0;
private int compare = 0;
private int p = 10;
private int[] b;
//private int[] ex;

public SortUtility() {
    num = new int[p];

}

public SortUtility(int[] Startnum) {
    num = Startnum;
}

public int[] createRandomArray(int max, int min) {
    //private int [10] ex;
    int [] ex = new int[num.length];
    for (int i = 0; i < num.length; i++) {
        num[i] = rand.nextInt(max + 1 - min) + min;

    }
    for (int i = 0; i < num.length; i++) {
        ex[i] = num[i];
    }
    return ex;
}

public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1; a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
                System.out.println(num[2]);
            }
        }
        printItems();
    } while (c != num.length);
    return num;
}

public void printItems() {
    System.out.print("\nPass " + c + " Compares " + compare + " Swaps " + swap + " " + "items ");
    for (int i = 0; i < num.length; i++) {
        System.out.print(num[i] + " ");
    }
}

public int[] sortArray2(int[] ex) {
    int a;
    int swap2 = 0;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1 - (c - 1); a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
            }
        }
        if (swap2 < swap && swap2 != swap) {
            if (swap2 < swap) {
                swap2 = swap;
            }
        }
        swap2++;
        printItems();
    } while (swap + 2 != swap2);
    return num;
}
}
import java.util.Random;
公共类SortClient{
公共静态void main(字符串[]args){
int[]num={25,7,99,14,55,3,47,6,1};
排序能力ds=新的排序能力();
//排序j=新的排序(l);
分拣率ab=新分拣率(num);
int[]a=ab.createRandomArray(99,0);
for(int i=0;inum[a+1]){
int temp=num[a];
num[a]=num[a+1];
num[a+1]=温度;
swap++;
System.out.println(num[2]);
}
}
printItems();
}而(c!=num.length);
返回num;
}
公共项目(){
System.out.print(“\nPass”+c+“比较”+compare+“交换”+swap+“+”项目”);
for(int i=0;inum[a+1]){
int temp=num[a];
num[a]=num[a+1];
num[a+1]=温度;
swap++;
}
}
if(swap2
在类sortutibility中,有一个名为“sortArray1”的方法。注意,它接收一个名为“ex”的数组作为参数。另外,请注意,它对“num”数组执行排序(num用0初始化)。因此,您的方法正在工作,但排序是在一个0的数组上进行的,而不是在刚刚作为参数接收的数组上进行的

因此,为了使它们正确地协同工作,在前面提到的方法中,将引用从“num”数组更改为“ex”数组

最后,变更后的上述方法为:

public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < ex.length - 1; a++) {
            compare++;
            if (ex[a] > ex[a + 1]) {
                int temp = ex[a];
                ex[a] = ex[a + 1];
                ex[a + 1] = temp;
                swap++;
                System.out.println(ex[2]);
            }
        }
        printItems();
    } while (c != ex.length);
    return ex;
}
public int[]sortArray1(int[]ex){
INTA;
printItems();
做{
C++;
对于(a=0;aex[a+1]){
int temp=ex[a];
ex[a]=ex[a+1];
ex[a+1]=温度;
swap++;
System.out.println(ex[2]);
}
}
printItems();
}而(c!=ex.长度);
退换货;
}
请,看看这些变化是否会对您的逻辑产生任何副作用


附言:你使用的冒泡排序很慢。考虑使用合并排序、堆排序和快速排序。查看此网站。

在排序循环之前,第一眼看到您的方法。请注意,您传递的原始数组将以这种方式排序(因为它将指向同一个内部引用),因此,如果要保留它,请将其替换为

,然后打印出随机数组并对其进行排序,您是否忘记了第二次打印?这里存在主要变量名称混淆。例如,在排序方法中,取一个名为
ex
的数组,然后打印并排序一个名为
num
的数组。在创建方法中,您使用名为
num
的局部数组来隐藏类变量,填充该数组并将其复制到另一个名为
ex
的局部数组中。您的代码无法运行的原因是因为您在几乎每个地方都使用了错误的变量。了解范围和阴影。
public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1; a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
                System.out.println(num[2]);
            }
        }
        printItems();
    } while (c != num.length);
    return num;
}
 num = ex; 
public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1; a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
                System.out.println(num[2]);
            }
        }
        printItems();
    } while (c != num.length);
    return num;
}
 num = ex; 
 num = ex; 
 System.arraycopy(ex, 0, num, 0, ex.length);