import java.util.ArrayList;

/**
 * This program demonstrates set operations using bit strings.
 * The program includes methods for complement, union, intersection, and difference of sets.
 * It also provides a method to convert an array of elements into a bit string.
 *
 * @author XiaoyinZhao
 * @version 1.0
 */
public class Lab1 {
    public static int[] attr = new int[]{1,2,3,4,5,6,7,8,9,10};
    public static int[] list = new int[]{1,1,1,1,1,1,1,1,1,1};
    public static int[] attr1 = new int[]{2,3,4,5,6};
    public static int[] attr2 = new int[]{1,2,4,6,7,8};

    /**
     * Calculates the complement of two sets given their bit string representations.
     *
     * @param list1 The bit string representation of set A.
     * @param list2 The bit string representation of set B.
     */
    public static void Complement(int[] list1, int[] list2){
        ArrayList<Integer> resultA = new ArrayList<>();
        ArrayList<Integer> resultB = new ArrayList<>();
        for(int i = 0; i < list.length; i++){
            if(list1[i] == 0){
                resultA.add(attr[i]);
            }
        }
        System.out.println('The Complement of A is: ' + resultA);
        for(int i = 0; i < list.length; i++){
            if(list2[i] == 0){
                resultB.add(attr[i]);
            }
        }
        System.out.println('The Complement of B is: ' + resultB);
    }

    /**
     * Calculates the union of two sets given their bit string representations.
     *
     * @param list1 The bit string representation of set A.
     * @param list2 The bit string representation of set B.
     */
    public static void Union(int[] list1, int[] list2){
        ArrayList<Integer> result = new ArrayList<>();
        int[] union = new int[]{0,0,0,0,0,0,0,0,0,0};
        for(int i = 0; i < union.length; i++){
            if(list1[i] == 1 || list2[i] == 1){
                union[i] = 1;
            }
        }
        for(int i = 0; i < list.length; i++){
            if(union[i] == 1){
                result.add(attr[i]);
            }
        }
        System.out.println('The Union of A and B is: ' + result);
    }

    /**
     * Calculates the intersection of two sets given their bit string representations.
     *
     * @param list1 The bit string representation of set A.
     * @param list2 The bit string representation of set B.
     */
    public static void Intersection(int[] list1, int[] list2){
        ArrayList<Integer> result = new ArrayList<>();
        int[] intersection = new int[]{0,0,0,0,0,0,0,0,0,0};
        for(int i = 0; i < intersection.length; i++){
            if(list1[i] == 1 && list2[i] == 1){
                intersection[i] = 1;
            }
        }
        for(int i = 0; i < list.length; i++){
            if(intersection[i] == 1){
                result.add(attr[i]);
            }
        }
        System.out.println('The Intersection of A and B is: ' + result);
    }

    /**
     * Calculates the difference between two sets given their bit string representations.
     *
     * @param list1 The bit string representation of set A.
     * @param list2 The bit string representation of set B.
     */
    public static void Difference(int[] list1, int[] list2){
        ArrayList<Integer> result = new ArrayList<>();
        int[] difference = new int[]{0,0,0,0,0,0,0,0,0,0};
        for(int i = 0; i < difference.length; i++){
            if(list1[i] != list2[i]){
                difference[i] = 1;
            }
        }
        for(int i = 0; i < list.length; i++){
            if(difference[i] == 1){
                result.add(attr[i]);
            }
        }
        System.out.println('The Difference of A and B is: ' + result);
    }

    /**
     * Converts an array of elements into a bit string representation.
     *
     * @param list The bit string representation of the set.
     * @param arr  The array representing the set elements.
     */
    public static void BitString(int[] list, int[] arr){
        for(int i = 0; i < attr.length; i++){
            for(int j = 0; j < arr.length; j++){
                if(attr[i] == arr[j]){
                    list[i] = 1;
                }
            }
        }
        System.out.print('[ ');
        for(int i = 0; i < list.length; i++){
            System.out.print(list[i] + ' ');
        }
        System.out.println(']');
    }

    /**
     * The main method to test the set operation methods.
     *
     * @param args A reference to a string array containing command-line arguments.
     */
    public static void main(String[] args){
        // Create two arrays to get bit strings
        int[] list1 = new int[]{0,0,0,0,0,0,0,0,0,0};
        int[] list2 = new int[]{0,0,0,0,0,0,0,0,0,0};

        // Use the method to get bit strings
        BitString(list1, attr1);
        BitString(list2, attr2);

        // Get the complement, union, intersection, difference
        Complement(list1, list2);
        Union(list1, list2);
        Intersection(list1, list2);
        Difference(list1, list2);
    }
}
Java实现集合操作:并集、交集、补集和差集

原文地址: http://www.cveoy.top/t/topic/oym 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录