【问题标题】:Sorting Array Class not working排序数组类不起作用
【发布时间】:2013-12-14 11:25:50
【问题描述】:

我写了一个类,它应该创建一个随机数组,并对其进行排序。我为每个方法编写了方法,每个方法中的代码都可以独立运行。但是当我一起使用它们时,我无法使用 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;
    }
}

这是我的客户

package sortclient;

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();
        int[] a = ab.createRandomArray(99, 0, num);

        for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }
    ab.sortArray1(a);

    }
}

这是我运行客户端时的结果:

run:
52
8
82
87
93
29
94
33
46

Pass 0 Compares 0 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 1 Compares 9 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 2 Compares 18 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 3 Compares 27 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 4 Compares 36 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 5 Compares 45 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 6 Compares 54 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 7 Compares 63 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 8 Compares 72 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 9 Compares 81 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 10 Compares 90 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 BUILD SUCCESSFUL (total time: 0     seconds)

这证明了随机数组方法有效并且排序有点有效

【问题讨论】:

  • 您打印出随机数组,然后对它进行排序,您是否忘记了第二次打印?
  • 您在这里有主要的变量名称混淆。例如,在 sort 方法中,您获取一个名为 ex 的数组,然后打印并排序一个名为 num 的数组。在创建方法中,您获取一个名为num 的本地数组,它会隐藏您的类变量,填充该数组并将其复制到另一个名为ex 的本地数组中。您的代码无法正常工作的原因是您几乎在每个地方都使用了错误的变量。了解范围和阴影。

标签: java arrays class sorting methods


【解决方案1】:

乍一看你的方法

 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;
}  
在 SortUtility 中没有使用提供的数组参数 ex,并且您正在对内部数组进行排序(这是另一个问题,为什么要保留它,然后尝试排序一个不同的传递数组)。一个快速的解决方法是在排序循环之前添加
  num = ex;  
。请注意,您传递的原始数组将被排序(因为它将指向相同的内部引用),因此如果您想保留它,请将
  num = ex;  
替换为
  System.arraycopy(ex, 0, num, 0, ex.length);  

【讨论】:

    【解决方案2】:

    在您的 SortUtility 类中,有一个名为“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;
    }
    

    请看看这些更改是否会对您的逻辑产生任何副作用。

    P.s.:您使用的冒泡排序很慢。考虑使用归并排序、堆排序和快速排序。查看此网站http://www.sorting-algorithms.com/

    【讨论】:

      【解决方案3】:

      您编写的代码是错误的。首先改变

           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;
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        • 2018-09-13
        相关资源
        最近更新 更多