在不使用Collections.sort的情况下对ArrayList中的对象进行排序

问题描述:

我想使用我自己的排序方法而不是 Collections.sort ,以便我可以修改我的程序以了解其他种类,泛型和 ArrayList 更好。

I would like to use my own sorting method instead of Collections.sort so that I can tinker around with my program to understand other sorts, generics, and ArrayLists better.

我有一个有员工编号成员的员​​工类。我知道如何制作一个 ArrayList 的Employee对象,但是你能解释一下我如何打印和排序它们吗?我从排序常规数组开始,并希望对Employee对象的ArrayList(员工编号)执行相同的操作。我无法理解如何打印对象的ArrayLists并对它们进行排序。

I have an employee class that has an employee number member. I know how to make an ArrayList of Employee objects, but could you explain how I could print and sort them? I started off by sorting a regular array and wanted to do the same with an ArrayList of Employee objects (the employee number). I'm having trouble understanding how to print ArrayLists of objects and sorting them.

package dataStructures;

import java.util.ArrayList;
import java.util.Arrays;

public class SortPractice {

    public static void main(String[] args) {
        int[] nums = {5,4,3,2,1};

        System.out.println(Arrays.toString(nums));

        BubbleSort1(nums);

        ArrayList<Employee> empList = new ArrayList<Employee>();

        for (int i=0; i<10; i++) {
            empList.add(new Employee(10-i));

        }

        BubbleSort(empList);  //This method doesn't work. I need help here.

    }


public static void BubbleSort (int[] A) {   //I included this because I know it works.
        int temp = 0;
        int firstLoopCount = 0;
        int SecLoopCount = 0;
        for (int i=0; i< A.length-1; i++) {
            firstLoopCount++;
            System.out.println(Arrays.toString(A) + i + " << First Loop interation");

           for (int j=0; j<A.length-1; j++) {
               if (A[j] > A[j+1]) {
                    temp = A[j];
                    A[j] = A[j+1];
                    A[j+1] = temp;
                }
               SecLoopCount++;
             System.out.println(Arrays.toString(A) + j + "  << Second Loop Interation");

           }
        }
        System.out.println((firstLoopCount+SecLoopCount));


    }

    public static void BubbleSort (ArrayList<Employee> empList) { //I tried to use the same
        int temp = 0;                                             //approach just with the List
        int firstLoopCount = 0;
        int SecLoopCount = 0;
        for (int i=0; i<empList.size()-1; i++) {
            firstLoopCount++;
            System.out.println(Arrays.toString(empList) + i + " << First Loop interation");

           for (int j=0; j<empList.size()-1; j++) {
               if (empList.get(j) > empList.get(j+1)) {    //I get errors here in Eclipse and 
                    temp = A[j];                           //up above when I use toString
                    A[j] = A[j+1];
                    A[j+1] = temp;
                }
               SecLoopCount++;
             System.out.println(Arrays.toString(A) + j + "  << Second Loop Interation");

           }
        }
        System.out.println((firstLoopCount+SecLoopCount));


    }

这是员工类。它有其他的getter和setter但我没有包含它们。

Here is the employee class. It has other getters and setters but I didn't include them.

package dataStructures;

 public class Employee {

     private int empNum;
     private String firstName;
     private String LastName;
     private String email;

     public Employee(int empNum) {
         this.empNum = empNum;
     }


     public String toString(){
         return " "+ empNum + ",";

     }
     public Employee() {

     }

     public int getEmpNum() {
        return empNum;
    }
    public void setEmpNum(int empNum) {
        this.empNum = empNum;
    }


访问数组与访问 ArrayList 不同。这是因为这两个对象根本不同。

Accessing an array is different from accessing an ArrayList. This is because these two objects are fundamentally different.

让我们专注于这行代码:

Let's focus on this line of code:

System.out.println(Arrays.toString(empList) + i + " << First Loop interation");

您将要为 Java 7 API ,以便您可以引用这些方法实际作为参数的内容。相信我,从长远来看,它会为你节省大量时间。

You're going to want to bookmark the Java 7 API so that you can reference what it is these methods actually take as arguments. Believe me, it will save you lots of time in the long run.

具体来说,代码无效,因为 toString 不接受 ArrayList 类型的参数。你可以直接打印一个 ArrayList ,因为它有一个合理的 toString 方法,而数组没有(这就是为什么你使用 Arrays#toString ):

Specifically, the code is invalid because toString does not accept a parameter of type ArrayList. You can just straight-up print an ArrayList, as it has a reasonable toString method, whereas an array doesn't (which is why you use Arrays#toString):

System.out.println(empList.toString() + i + " << First Loop interation");

让我们看看下面的 if 块:

Let's look at this if block next:

if (empList.get(j) > empList.get(j + 1)) {    //I get errors here in Eclipse and
    temp = A[j];                           //up above when I use toString
    A[j] = A[j + 1];
    A[j + 1] = temp;
}

我会直言不讳,你会在具有该代码的任何合理的IDE。原因是:您使用括号索引数组,但是使用获取获取 ArrayList

I'll be blunt, you're going to get errors in any reasonable IDE with that code. The reason: you index into arrays with brackets, but you use get for an ArrayList.

第一个问题是您无法将这两个实例与> 进行比较。你要做的就是检索你想要与之比较的字段。

The first fix is that you can't compare those two instances with >. What you'd wind up doing instead is retrieving the field you want to compare it with instead.

if(empList.get(j).getEmpNum() > empList.get(j+1).getEmpNum()) {
    // more code
}

这是 ArrayList 的相关Javadoc。你将需要它。

Here's the relevant Javadoc for ArrayList. You're going to need it.

如果,让我们关注的内部部分。您在那里进行的操作称为交换。您从一个位置获取元素并用另一个位置覆盖它。由于数组不会向下移动元素,因此必须在覆盖它之前捕获原始值。

Let's focus on the inner part of the if. The operation you're doing there is called a swap. You're taking an element from one location and overwriting it with another. Since arrays don't shift elements down, you have to capture the original value before you overwrite it.

用英语表示:


  • 取原值

  • 将新值放入原始值的原始数组位置

  • 放置原始值新值的原始数组位置中的值

您不应该使用 ArrayList ,因为它可以在特定位置添加元素

You shouldn't have to do that with an ArrayList, as it can add the element in a specific spot.

在英语中,它应该如下所示:

In English, it should be as simple as:


  • 在原始值的位置插入新值

  • 在列表中删除新值

在Java中,它可能如下所示:

In Java, it might read like this:

if(empList.get(j).getEmpNum() > empList.get(j + 1).getEmpNum()) {
    empList.add(j, empList.get(j + 1));
    empList.remove(j + 1);
}