有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在ArrayList中对对象进行排序,而不使用集合。分类

我想使用我自己的排序方法而不是Collections.sort,这样我就可以修改我的程序,更好地理解其他排序、泛型和ArrayList

我有一个employee类,它有一个employee number成员。我知道如何制作Employee对象的ArrayList,但是你能解释一下我如何打印和排序它们吗?我首先对一个常规数组进行排序,并希望对Employee对象的ArrayList(员工编号)进行同样的排序。我很难理解如何打印对象的数组列表并对它们进行排序

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


    }

这是employee类。它还有其他的接球手和二传手,但我没有包括他们

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

共 (2) 个答案

  1. # 1 楼答案

    下面是一个例子。在这种情况下,您的类必须提供一个方法来覆盖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;
            }
        }
    }
    
  2. # 2 楼答案

    我注意到的一个问题是:

    empList.get(j) > empList.get(j+1)
    

    您正在比较两个对象,即两个employee对象,这通常只用于基本类型(例如Integer)

    你可能想比较的是员工ID,我假设是在你的员工中。java文件(请发布此文件以便我们查看)。这是一个你可以为这条线做些什么的例子-

    empList.get(j).getEmployeeId() > empList.get(j+1).getEmployeeId()
    

    编辑:抱歉,问题读错了,没有使用集合。排序()