如何在Java中为集合编写交集和联合方法

时间:2014-04-26 18:15:19

标签: java set set-intersection set-union

我是新手使用套件和其中一个练习来学习如何使用套件,我已经获得了一个模板文件“SetInt”和一个完整的测试文件“TestSetInt”。我知道我应该使用retainAll()和addAll()但是测试类要求我编写方法。任何人都知道如何编写这两种方法?任何帮助将不胜感激。

SetInt是我必须添加方法的文件,如下所示:

public class SetInt 
{
    int[] setArray = new int[52];

public void add(int a)
{
    if (a < setArray.length-2)
    {
        setArray[a] = a;
    }
}
/* add a to the set in the correct position
 *@param a the value to be added
 **/    

public SetInt intersection(SetInt anySet)
{       
    return anySet;
}
/* returns the set of integers common to both
 * the set passed in as an argument and the set the
 * method is called on
 * @param anySet - the set to be intersected
 * @return a set containing the result of the intersection
*/

public SetInt union(SetInt anySet)
{   
    return anySet;
}
/* returns the set of integers which are in either the 
 * set passed in as an argument or the set the method
 * is called on
 * @param anySet - one of the sets in the union
 * @return a set containing the result of the union
*/

}

这是TestSetInt,一个为我提供的文件,我必须运行该文件以查看我的方法是否正常工作:

public class TestSetInt
{
    public TestSetInt()
    {
        SetInt test1 = new SetInt();
        SetInt test2 = new SetInt();

//      adding numbers to the 2 sets
        for(int i = 2; i<49;i=i+3)
        {
            test1.add(i);
        }
        System.out.println();

        for(int i = 2; i<49;i=i+4)
        {
            test2.add(i);
        }
        System.out.println();

//      testing intersection
        SetInt interx = new SetInt();
        interx=test2.intersection(test1);

//      testing union       
        SetInt unionx = test1.union(test2);

    }

    public static void main (String args[])
    {
        TestSetInt practice = new TestSetInt(); 
    }
}

1 个答案:

答案 0 :(得分:0)

这有效:)

SetInt Class

public class SetInt {
    int[] setArray = new int[52];

    public void add(int a) {
        if (a < setArray.length - 2) {
            setArray[a] = a;
        }
    }

    /*
     * add a to the set in the correct position
     * 
     * @param a the value to be added
     */

    public SetInt intersection(SetInt anySet) {
        SetInt temp = new SetInt();
        for (int i = 0; i < setArray.length; i++) {
            for (int j = 0; j < anySet.setArray.length; j++) {
                if (setArray[i] == anySet.setArray[j]) {
                    temp.setArray[i] = setArray[i];
                }
            }
        }
        return temp;
    }

    /*
     * returns the set of integers common to both the set passed in as an
     * argument and the set the method is called on
     * 
     * @param anySet - the set to be intersected
     * 
     * @return a set containing the result of the intersection
     */

    public SetInt union(SetInt anySet) {
        SetInt temp = new SetInt();
        for (int i = 0; i < setArray.length; i++) {
            for (int j = 0; j < anySet.setArray.length; j++) {
                temp.setArray[i] = setArray[i];
                if (anySet.setArray[j] != 0) {
                    temp.setArray[j] = anySet.setArray[j];
                }
            }
        }
        return temp;
    }
    /*
     * returns the set of integers which are in either the set passed in as an
     * argument or the set the method is called on
     * 
     * @param anySet - one of the sets in the union
     * 
     * @return a set containing the result of the union
     */

}

<强> TestSetInt

public class TestSetInt {
    public TestSetInt() {
        SetInt test1 = new SetInt();
        SetInt test2 = new SetInt();

        // adding numbers to the 2 sets
        for (int i = 2; i < 49; i = i + 3) {
            test1.add(i);
        }
        printSetInt(test1);

        for (int i = 2; i < 49; i = i + 4) {
            test2.add(i);
        }
        printSetInt(test2);

        // testing intersection
        SetInt interx = new SetInt();
        interx = test2.intersection(test1);
        printSetInt(interx);

        // testing union
        SetInt unionx = test1.union(test2);
        printSetInt(unionx);

    }

    public void printSetInt(SetInt set) {
        for (int i : set.setArray) {
            System.out.print(i + ",");
        }
        System.out.println();
    }

    public static void main(String args[]) {
        TestSetInt practice = new TestSetInt();
    }

}