在不使用 Collections.sort 的情况下对 ArrayList 中的对象进行排序 [英] Sorting objects in an ArrayList without using Collections.sort

查看:24
本文介绍了在不使用 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.

我有一个员工类,它有一个员工编号成员.我知道如何制作 Employee 对象的 ArrayList,但是你能解释一下我如何打印和排序它们吗?我首先对常规数组进行排序,并希望对 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.

让我们关注这行代码:

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 使用 get.

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/代码>.你会需要它.

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

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

这篇关于在不使用 Collections.sort 的情况下对 ArrayList 中的对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆