使用Comparable排序整数数组

山姆

我正在一个项目中,我必须使用Comparable对一个整数对象数组进行排序。

我的add方法采用类型E的项。如果我的size变量(用于跟踪数组theData []中的元素)= 0(将其初始化为),则只需将其放入theData [0]中即可。

如果不是,则使用item.compareTo将项目与数组中已存在的每个项目进行比较。如果对于数组中的某个数字,compareTo的结果<0,则将所有内容移至该数字之后并向右移动,然后在其之前插入该项目。

如果compareTo返回0,则表示该项等于数组中的数字,我什么也不做,因为我不想在数组中重复。

如果循环中没有compareTo语句返回-1或0,则将该项放在数组末尾的Data [size]中,因为它必须大于所有其他数字。

但是,这不起作用。每当我创建一个新的Set并向其中添加一些数字,然后尝试使用for循环输出我的set的内容时,我都会不断遇到java.lang.ArrayIndexOutOfBoundsException:此行出现10个错误:

theData[j + 1] = theData[j];

我尝试从头开始,并用不同的逻辑重新编写循环,每次我碰到这堵墙时。我知道我要么必须正确地移位,要么不能使用reallocate方法正确地增加数组的大小,但是我无法将其包裹在头上。

import java.util.*;

public class Set<E extends Comparable<E>> {

String s;
String name;
private static final int INITIAL_CAPACITY = 10;
private E[] theData;
private int size = 0;
private int capacity = INITIAL_CAPACITY;

@SuppressWarnings("unchecked")
public Set() {
    theData = (E[]) new Comparable[capacity];
}

public Set(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public void add(E item) {

    if (size == capacity) {
        reallocate();
    }

    if (size == 0) {                        // If size is 0, add item to theData[0]
        theData[size] = item;
        size++;
        return;
    }

    else {                                  // Else compare the item to every item in loop.
        for (int i = 0; i < size; i++) {

            int result = item.compareTo(theData[i]);

            if (result < 0) {       

                for (int j = 0; j < size; j++) {        //If item is less than a number, shift everything
                    theData[j + 1] = theData[j];        //after that index to the right, and add item
                    theData[j] = item;

                }
            }

            if (result == 0) {
                return;
            }

            else {                                      //If item is not less than or equal to any
                theData[size] = item;                   //numbers in the array, add it to the end
                size++;

            }

        }

    }

}

/*
 * if (size>=1){ int result = item.compareTo(theData[size-1]); if(result<0){
 * E temp = theData[size-1]; theData[size-1] = item; theData[size] = temp; }
 * if(result>1){ return; } }
 */

public E get(int index) {
    if (index < 0 || index >= size) {
        throw new ArrayIndexOutOfBoundsException(index);
    }
    return theData[index];
}

public int size() {
    return size;
}

private void reallocate() {
    capacity = 2 * capacity;
    theData = Arrays.copyOf(theData, capacity);
}

}

编辑:我用来测试它的驱动程序方法-

public class Driver {

String one = "two";

public static void main(String[] args){

Set<Integer> one = new Set<Integer>();

one.add(63);
one.add(20);
one.add(127);
one.add(10);
one.add(26);
one.add(15);

for(int i = 0; i < one.size(); i++){
System.out.println(one.get(i));
}

}
}

当时j == size - 1theData[j+1]将把您带出阵列。

您想在结束之前循环到一个。

 for (int j = 0; j < size - 1; j++) {        //If item is less than a number, shift everything
     theData[j + 1] = theData[j];        //after that index to the right, and add item
     theData[j] = item;
}

因此,我也研究了插入操作所具有的逻辑,但这一点都不合理。为什么要完全延迟插入?如果您有房间,只需添加它!

接下来,双循环本质上实现了冒泡排序,但是它有一个致命的缺陷:您永远不会完成交换;您只会反复覆盖您的价值观。您也没有朝着正确的方向进行比较;如果您要从数组开头开始则要交换左侧的值大于右侧的值

因此,...这就是实现的形式...

public void add(E item) {

    if (size == capacity) {
        reallocate();
    }

    theData[size++] = item;
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - 1; j++) {
            if (theData[j].compareTo(theData[j + 1]) > 0) {
                // perform the swap (you need an extra variable!
            }
        }
    }
}

我将实现交换作为练习留给读者。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章