【问题标题】:how to swap two array objects in java如何在java中交换两个数组对象
【发布时间】:2015-12-07 03:58:16
【问题描述】:

我正在尝试对课程进行排序并按课程编号组织它们。 我创建了一个对象数组,其中包含 3 个属性(部门、编号、标题)。我想使用选择排序方法按“num”对这个数组进行排序。当我尝试交换两个数组时,编译器说 int 不能转换为 Course[]。

public static void sortByNumber(Course[] arr){

    int size = arr.length;
    for (int i = 0; i < size - 1; i++) {
        int min = i;
        for (int j = i + 1; j < size; j++) {
            if (arr[j].getNum() < arr[min].getNum()) {
                min = j;
            }
        }
        int temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;
    }
}

这是我的另一堂课。

public class Course {

    //INSTANCE VARIABLES
    private String dept = "";
    private int num = 0;
    private String title = "";

    //CONSTRUCTORS
    public Course(String dept, int num) {
        this.dept = dept;
        this.num = num; 
    }

    public Course(String dept, int num, String title) {
        this.dept = dept;
        this.num = num; 
        this.title = title;
    }

    public Course() {
        this.dept = "AAA";
        this.num = 100;
        this.title = "A course";    
    }

    //SETTER AND GETTER METHODS
    public void setDept(String dept) {
        this.dept = dept;   
    }

    public void setNum(int num) {
        this.num = num;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDept() {
        return this.dept;   
    }

    public int getNum() {
        return this.num;    
    }

    public String getTitle() {
        return this.title;  
    }

}

【问题讨论】:

    标签: java arrays class


    【解决方案1】:
    int temp = arr[i];
    

    arr[i]Coursetempint。您不能将Course 分配给int 变量,也不能将int 分配给Course 变量,因为它们是两种完全不同的类型。

    将您的temp 设为Course

    Course temp = arr[i];
    

    【讨论】:

      【解决方案2】:

      伊沙梅尔的回答是正确的。我将放弃这个添加,因为稍后您可能会发现允许用户按任何字段排序非常有用:

      首先,您需要将 Comparable 接口添加到 Course 类并编写 compareTo() 方法。我在类中添加了一个枚举,以匹配您可能想要排序的每个字段:

      public class Course implements Comparable<Course>{
      
          public static enum SortBy {
              DEPARTMENT,
              COURSE_NUM,
              TITLE,
              NOT_SORTED;
          }
      
          public static SortBy sortBy = SortBy.NOT_SORTED; 
      
          @Override
          public int compareTo(Course course) {
              if(sortBy == SortBy.DEPARTMENT) return getDept().compareTo(course.getDept());
              if(sortBy == SortBy.COURSE_NUM) return getNum() - course.getNum();
              if(sortBy == SortBy.TITLE) return getTitle().compareTo(course.getTitle());
              return 0;
          }
      ...
      

      您现在可以将排序方法中的“if”语句修改为:

      if (arr[j].compareTo(arr[min]) < 0) {
          min = j;
      }
      

      这是一个现在使用它的例子......

      public static void main(String[] args) {
          Course[] arr = {
                      new Course("dep-B", 3, "title-F"),
                      new Course("dep-C", 1, "title-E"),
                      new Course("dep-A", 2, "title-D")
          };
      
      
          System.out.println("Sorted by default (not sorted)");
          System.out.println(Arrays.toString(arr));
      
          System.out.println("Sorted by Department");
          Course.sortBy = SortBy.DEPARTMENT;
          sortByNumber(arr);
          System.out.println(Arrays.toString(arr));
      
          System.out.println("Sorted by Course Number");
          Course.sortBy = SortBy.COURSE_NUM;
          sortByNumber(arr);
          System.out.println(Arrays.toString(arr));
      
          System.out.println("Sorted by Title");
          Course.sortBy = SortBy.TITLE;
          sortByNumber(arr);
          System.out.println(Arrays.toString(arr));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        相关资源
        最近更新 更多