intergerSet实现Comparable

时间:2014-05-08 05:14:09

标签: java comparable implements

该项目的目的是:

创建一个名为IntegerSet的类,该类实现可在简单集应用程序中使用的Comparable接口。 IntegerSet类的每个对象都可以保存0到255范围内的正整数,并且大多数常见的set操作都可用。一个集必须在内部表示为1和0(或布尔数组)的int数组。如果整数i在集合中,则数组元素a [i]为1,如果整数j不在集合中,则[j]为0。

然后添加一些成员方法来修改它。

我不熟悉"实现Comparable接口,"但这是我到目前为止所得到的。驱动程序无法测试它,它显示"线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:256"

有人可以帮我找出问题所在吗?谢谢!

import java.util.Arrays;
public class IntegerSet implements Comparable
{   
private int L = 256;
private int[] a = new int[L];

public IntegerSet()
{
    Arrays.fill(a, 0);
}

public IntegerSet(int n)
{
    a[n] = 1;
}

public IntegerSet(int[] n)
{
    a = n;
}


public IntegerSet union(IntegerSet other)
{
    int[] c = new int[L];
    for(int i = 0; i < L; i++)
    {
        if(this.a[i]==1 || other.a[i] ==1)
            c[i] = 1;
        else
            c[i] = 0;
    }
    IntegerSet temp = new IntegerSet(c);
    return temp;
}

public IntegerSet intersection(IntegerSet other)
{
    int[] c = new int[L];
    for(int i = 0; i < L; i++)
    {
        if(this.a[i]==1 && other.a[i] ==1)
            c[i] = 1;
        else
            c[i] = 0;
    }
    IntegerSet temp = new IntegerSet(c);
    return temp;
}

public IntegerSet difference(IntegerSet other)
{
    int[] c = new int[L];
    for(int i = 0; i < L; i++)
    {
        if(this.a[i] != other.a[i])
            c[i] = 1;
        else
            c[i] = 0;
    }
    IntegerSet temp = new IntegerSet(c);
    return temp;
}

public IntegerSet insertElement(int k)
{
    a[k] = 1;
    IntegerSet temp = new IntegerSet(a);
    return temp;
}

public IntegerSet removeElement(int k)
{
    a[k] = 0;
    IntegerSet temp = new IntegerSet(a);
    return temp;
}

public boolean isElement(int k)
{
    return(a[k] == 1);
}

public String toString()
{
    String str = "";
    for(int i = 0; i < L; i++)
    {
        if(a[i] == 1)
            str += (i + ", ");
    }
    return "{" + str + "}";
}

public IntegerSet copy()
{
    IntegerSet temp = new IntegerSet(a);
    return temp;
}

public boolean subset(IntegerSet other)
{
    boolean sub = true;
    int i = 0;
    while(sub)
    {
        if(this.a[i] == 1)
        {
            if(other.a[i] == 1)
                sub = true;
            else
                sub = false;
        }
        i++;
    }
    return sub;
}

public boolean superset(IntegerSet other)
{
    boolean sup = true;
    int i = 0;
    while(sup)
    {
        if(other.a[i] == 1)
        {
            if(this.a[i] == 1)
                sup = true;
            else
                sup = false;
        }
        i++;
    }
    return sup;
}

public void addAll()
{
    Arrays.fill(a, 1);
}

public void removeAll()
{
    Arrays.fill(a, 0);
}

public int compareTo(Object other) 
{
    // TODO Auto-generated method stub
    return 0;
}

}

和司机:

public class IntegerSetDriver 
{
    public static void main(String[] args) 
    {
        IntegerSet s1, s2, s3, s4, s5; 
        s1 = new IntegerSet(); // s1 is an empty set, {} 
        s2 = new IntegerSet(5); // s2 is a set with one element, {5} 
        s1.insertElement(1); // s1 is now {1} 
        s3 = s1.copy(); // s3 is now {1} 
        s4 = s1.union(s2); // s4 is now {1, 5} and s1 is still {1} 
        s5 = s4.insertElement(8).removeElement(5); // s4 is now {1, 8} // s5 references s4 

        int result = s3.compareTo(s4); // result is -1 (or < 0) 
        boolean yes = s3.subset(s4); // yes should be true 
        s5.removeAll(); // s4 and s5 both reference same empty set, {} 
       s1.removeElement(500); // invalid element so ignore, s1 is still {1}
    }
}

1 个答案:

答案 0 :(得分:0)

在添加/删除int[] a(即500不是有效索引)之前,您应检查边界。

另外,看看你的构造函数IntegerSet(input[] n),你不能直接&#34;分配&#34;就是这样。您需要解析数组并正确更新本地a[]

&#34; N&#34;值的范围为0-255,而&#34; a&#34;应该只是{1,0}。

其他一些事情也需要解决。