在不使用Collections.sort的情况下对ArrayList中的对象进行排序
我想使用我自己的排序方法而不是Collections.sort
以便我可以修改我的程序以更好地理解其他排序,generics和ArrayList
。
我有一个员工类,有一个员工编号成员。 我知道如何制作一个Employee对象的ArrayList
,但是你能解释一下我如何打印和排序它们吗? 我从排序常规数组开始,并希望对Employee对象的ArrayList(员工编号)执行相同的操作。 我无法理解如何打印对象的ArrayLists并对它们进行排序。
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 empList = new ArrayList(); 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[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 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.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但我没有包含它们。
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
不同。 这是因为这两个对象根本不同。
让我们关注这一行代码:
System.out.println(Arrays.toString(empList) + i + " << First Loop interation");
您将要为Java 7 API添加书签,以便您可以引用这些方法实际上作为参数的内容。 相信我,从长远来看,它将为您节省大量时间。
具体来说,代码无效,因为toString
不接受ArrayList
类型的参数。 你可以直接打印一个ArrayList
,因为它有一个合理的toString
方法,而一个数组没有(这就是你使用Arrays#toString
):
System.out.println(empList.toString() + i + " << First Loop interation");
我们来看看下一个块:
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中使用该代码获得错误。 原因:您使用括号索引数组,但是您使用get
作为ArrayList
。
第一个问题是您无法将这两个实例与>
进行比较。 你要做的就是检索你想要与之比较的字段。
if(empList.get(j).getEmpNum() > empList.get(j+1).getEmpNum()) { // more code }
这是ArrayList
的相关Javadoc 。 你需要它。
让我们关注if
的内部部分。 您在那里进行的操作称为交换。 您从一个位置获取元素并用另一个位置覆盖它。 由于数组不会向下移动元素,因此必须在覆盖原始值之前捕获原始值。
用英文说:
- 取原值
- 将新值放入原始值的原始数组位置
- 将原始值放在新值的原始数组位置
您不应该使用ArrayList
执行此操作,因为它可以在特定位置添加元素 。
在英语中,它应该如下:
- 在原始值的位置插入新值
- 删除列表中的新值
在Java中,它可能如下所示:
if(empList.get(j).getEmpNum() > empList.get(j + 1).getEmpNum()) { empList.add(j, empList.get(j + 1)); empList.remove(j + 1); }
我注意到的一个问题是这一行 –
empList.get(j) > empList.get(j+1)
您正在比较2个对象,即2个员工对象,除了原始类型(例如整数)之外,通常不使用它。
您可能想要比较的是我假设在Employee.java文件中的员工ID(请发布此文件以便我们查看)。 以下是您可以为此行做些什么的示例 –
empList.get(j).getEmployeeId() > empList.get(j+1).getEmployeeId()
编辑:抱歉读错了问题,没有使用Collections.sort()
这是一个例子。 在这种情况下,您的类必须提供一个方法来覆盖Comparable接口中的compareTo方法。 规范是,如果调用对象较大,则应返回大于0的整数;如果调用者较小,则返回小于0的整数,否则返回0。
public class Employee implements Comparable { //Rest of your class code here public void getID() { //return some value associated with the ID } //override this method public int compareTo(Employee other) { //code to compare two Employees // Maybe something like the following if (this.getID() > other.getID()) { return 1; } else if (this.getID() < other.getID()) { return -1; } else { return 0; } } }
这是@Makoto帮助下的最终答案
public static void BubbleSort (ArrayList empList) { for (int i=0; i empList.get(j+1).getEmpNum()) { empList.add(j, empList.get(j + 1)); //This line inserts the smaller value empList.remove(j+2); //into the first index and pushes the } //indices down 1. So I need to remove //j+2 not j+1. /*When I use the debugger to step into toString() it says source not found. I don't get it but it works.*/ System.out.println(empList.toString() + j + " << Second Loop Interation"); } System.out.println(empList.toString() + i + " << First Loop interation"); } }