创建一个insert()方法来保存有序数组

时间:2014-07-13 21:25:55

标签: java arrays sorting comparable

我试图制作insert()方法,使用compareTo方法保持有序数组的排序(没有排序算法,仅使用compareTo)。 它适用于list.insert(dd)list.instert(aa),但它不适用于list.instert(dd)list.insert(aa),我知道为什么但不知道如何修复它。 insert方法属于第二类。

我再次尝试使insert()方法保持已订购的有序数组。 例如:

int[] Array = {1,2,6,7,9};
Array.insert(3);
Array.insert(8);
System.out.println(Array); // should print out 1,2,3,6,7,8,9

至少如果我正在使用整数。在下面的代码中,我处理课程。

课程:

import java.io.Serializable;
import java.util.*;

class Course implements Comparable<Object>, Serializable {
    private String _prefix;
    private int _number;
    private String _department;
    private String _grade;

    public Course(String prefix, int number, String department, String grade) {
        _prefix = prefix;
        _number = number;
        _department = department;
        if (grade == null)
            _grade = "";
        else
            _grade = grade;
    }

    public Course(String prefix, int number, String Department) {
        this(prefix, number, Department, "");
    }

    public String getPrefix() {
        return _prefix;
    }

    public int getNumber() {
        return _number;
    }

    public String getDepartment() {
        return _department;
    }

    public String getGrade() {
        return _grade;
    }

    public void setGrade(String grade) {
        _grade = grade;
    }

    public boolean taken() {
        return !_grade.equals("");
    }

    public boolean equals(Object other) {
        boolean result = false;
        if (other instanceof Course) {
            Course otherCourse = (Course) other;
            if (_prefix.equals(otherCourse.getPrefix())
                    && _number == otherCourse.getNumber())
                result = true;
        }
        return result;
    }

    public int compareTo(Object o) {
        Course temp = (Course) o;

        if (_department.compareTo(temp.getDepartment()) == 0) {
            if (_number < temp.getNumber()) {
                return -1;
            } else if (_number > temp.getNumber()) {
                return 1;
            } else if (_number == temp.getNumber()) {
                if (_prefix.compareTo(temp.getPrefix()) == 0) {
                    return 0;
                } else if (_prefix.compareTo(temp.getPrefix()) == 1) {
                    return 1;
                } else if (_prefix.compareTo(temp.getPrefix()) == -1) {
                    return -1;
                }
            }

        } else if (_department.compareTo(temp.getDepartment()) > 0) {
            return 1;
        } else if (_department.compareTo(temp.getDepartment()) < 0) {
            return -1;
        }

        return 0;

    }

    public String toString() {
        String result = _prefix + " " + _number + ": " + _department;
        if (!_grade.equals(""))
            result += "  [" + _grade + "]";
        return result;
    }
}

CourseList:

class CourseList {
    private Course[] _courseList;
    private int _size = 0;

    public CourseList() {
        // setting the current size of the array to 0 since a list starts with 0
        // elements
        _courseList = new Course[_size];
    }

    public int size() {
        return _size;
    }

    public void insert(Course courseToInsert) {

        Course[] tempCourseList = new Course[_size + 1];

        if (_size == 0) // if size = 0
        {
            tempCourseList[0] = courseToInsert;
            _size++; // used to tell if we need to increment the size or not
        }

        else // if size > 0
        {
            for (int i = 0; i < _size; i++) {

                if (courseToInsert.compareTo(_courseList[i]) == -1) // if less
                    // than
                {
                    tempCourseList[i] = courseToInsert;

                    for (int j = i; j < _size; j++) {
                        tempCourseList[j + 1] = _courseList[i];
                        i++;
                    }
                    _size++;
                }

                else {
                    if (i == _size - 1) {
                        tempCourseList[i + 1] = courseToInsert;
                        _size++;
                    }

                    /*
                     * else if (i < _size) { for (int j = i; j < _size; j++) {
                     * tempCourseList[j+1] = _courseList[j]; } }
                     */
                }
            }
        }

        _courseList = tempCourseList;
    }

    public String toString() {
        String stringToReturn = "Contents of List:\n\n";

        for (Course a : _courseList) {
            stringToReturn += a + "\n";
        }

        return stringToReturn;
    }
}

驱动程序:

public class Driver {
    public static Scanner kb = new Scanner(System.in);

    public static void main(String[] args) {
        CourseList list = new CourseList();

        Course ff = new Course("IEE", 380, "Engineering");
        Course a = new Course("EEE", 230, "Engineering");
        Course b = new Course("MAT", 150, "Liberal Arts");
        Course c = new Course("PHY", 150, "Liberal Arts");
        Course d = new Course("PHI", 304, "Liberal Arts");
        Course e = new Course("ECN", 214, "W.P. Carey");
        Course f = new Course("EEE", 120, "Engineering");

        Course aa = new Course("PHI", 304, "AAAAAA");
        Course bb = new Course("ECN", 214, "BBBBBB");
        Course cc = new Course("EEE", 120, "CCCCCC");
        Course dd = new Course("EEE", 120, "DDDDDD");

        // System.out.println( aa.compareTo(aa) );
        // System.out.println( aa.compareTo(bb) );
        // System.out.println( bb.compareTo(aa) );

        String bbb = "BBB";
        String aaa = "AAA";

        /*
         * list.insert(dd); list.insert(cc); // works list.insert(bb);
         * list.insert(aa);
         */

        list.insert(aa);
        list.insert(bb);
        /*
         * list.insert(cc); list.insert(dd);
         */

        System.out.println(list);
    }
}

1 个答案:

答案 0 :(得分:-1)

你不需要在这里制作自己的方法,Arrays.binarySearch()会告诉你把它放在哪里。简单地说:

Course[] temp = new Course[_courseList.size()]
_courseList.toArray(temp);
_courseList.add(Math.abs(Arrays.binarySearch(temp,courseToInsert)) - 1,courseToInsert);

你已经完成了。不要重新发明轮子,几乎所有库中已存在的东西。如果你有java 1.8,你也可以删除临时数组。