如何在java中交换两个数组对象

时间:2015-12-07 03:58:16

标签: java arrays class

我正在尝试按课程编号排序课程。 我创建了一个包含3个属性(department,num,title)的对象数组。我想使用选择排序方法按'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;  
    }

}

2 个答案:

答案 0 :(得分:1)

arr[i]

CoursetempintCourse。您无法将int分配到int变量,也不能将Course分配到temp变量,因为它们是两种完全不同的类型。

让您Course成为Course temp = arr[i];

 z3 <- z -z2
 zcol <- sapply(z3, FUN=function(x) if(x>0) "red" else "blue")
 # Trick in here, see help(presp):
 # col the color(s) of the surface facets...
 # This is recycled to the (nx-1)(ny-1) facets
 zcol <- matrix(zcol, ncol=20)[1:19, 1:19]
 persp(x,y, z3, theta = 40, phi = 30, expand = 0.5, col = zcol , border= NA, zlab="", zlim=c(-1,1))

答案 1 :(得分:0)

Ishamael的回答是正确的。我将丢弃此添加内容,因为您稍后可能会发现允许用户按任何字段排序非常有用:

首先,您需要将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));
}