繁体   English   中英

intergerSet实现可比

[英]intergerSet implements Comparable

该项目的目的是:

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

然后添加一些成员方法对其进行修改。

我不熟悉“实现比较接口”,但是到目前为止,这是我得到的。 驱动程序无法对其进行测试,它显示“线程“ 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}
    }
}

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

另外,请查看构造函数IntegerSet(input[] n) ,您不能像这样直接“分配”它。 您需要解析数组并正确更新本地a[]

“ n”的取值范围为0-255,而“ a”的取值范围仅为{1,0}。

其他一些问题也需要修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM