Cara
Cara

Reputation: 25

how to swap two array objects in java

I'm trying to sort courses and organize them by course number. I have created an array of objects which contain 3 attributes (department, num, title). I want to sort this array by 'num' using the selection sort method. When i try to swap the two arrays the compiler says int cannot be converted to 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;
    }
}

this is my other class.

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

}

Upvotes: 1

Views: 753

Answers (2)

MCS
MCS

Reputation: 51

Ishamael's answer was correct. I will throw out this addition as you later may find it very useful to allow the user to sort by any field:

First you will need to add the Comparable interface to your Course class and write the compareTo() method. I've added an enum to the class to match each field you might want to sort by:

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

You can now modify your 'if' statement in your sort method to:

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

Here is an example of using it now...

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

Upvotes: 0

Ishamael
Ishamael

Reputation: 12795

int temp = arr[i];

arr[i] is Course, temp is int. You cannot assign a Course into an int variable, neither can you assign an int into a Course variable, because they are two completely different types.

Make your temp a Course:

Course temp = arr[i];

Upvotes: 1

Related Questions