Java 如何将对象数组转换为自定义类型数组(涉及数组列表)

Java 如何将对象数组转换为自定义类型数组(涉及数组列表),java,arraylist,set,Java,Arraylist,Set,我试图做的是使用自定义类型IntSet查找两个集合的交集。代码编译得很好,但是当我运行它时,它会抛出一个ArrayStoreException。据我所知,问题是试图将对象数组转换为IntSet。代码如下: public IntSet[] intersection(Integer[] s1, Integer[] s2) { Arrays.sort(s1);//sort arrays Arrays.sort(s2); IntSet[] s3; int s1in

我试图做的是使用自定义类型
IntSet
查找两个集合的交集。代码编译得很好,但是当我运行它时,它会抛出一个
ArrayStoreException
。据我所知,问题是试图将对象数组转换为
IntSet
。代码如下:

    public IntSet[] intersection(Integer[] s1, Integer[] s2) {
    Arrays.sort(s1);//sort arrays
    Arrays.sort(s2);
    IntSet[] s3;
    int s1index = 0;//initalise index and counters
    int s2index = 0;

    while (s1index < s1.length && s2index < s2.length) {
        if (s1[s1index] == s2[s2index]) {//if present in both arrays
            al.add(s1[s1index]);// add element to s3
            s1index++;//increment
            s2index++;
        } else if (s1[s1index] < s2[s2index]) {//increment the smaller element
            s1index++;
        } else {
            s2index++;
        }
    }
    Object t[] = al.toArray();//convert to array
    **s3 = Arrays.copyOf(t, t.length, IntSet[].class);**//convert to intSet//exception thrown here
    return s3;
public IntSet[]交叉点(整数[]s1,整数[]s2){
Arrays.sort(s1);//排序数组
数组。排序(s2);
IntSet[]s3;
int s1index=0;//初始化索引和计数器
int指数=0;
而(s1index
完整代码如下:

public class IntSet {

/**
 * @param args the command line arguments
 */
/*
 * SPECIFICATIONS: 1.addElement(elem) adds a new element to the set Pre:
 * integer elem != any other element in the set Post: NUMBER_ADDED returned
 * if not already present, NUMBER_ALREADY_IN_SET if it is.
 *
 * 2.removeElement(elem) removes an element from the set Pre: integer elem
 * is already in set Post: NUMBER_REMOVED returned if present,
 * NUMBER_NOT_IN_SET if not.
 *
 * 3.intersection(s1,s2) returns the intersection of two sets Pre: two
 * integer arrays s1 and s2 Post: array containing the elements common to
 * the two sets, empty set if there is nothing in common
 *
 * 4.union(s1,s2) returns the union of two sets Pre: two integer arrays s1
 * and s2 Post: array of non-duplicate elements present in both arrays.
 *
 * 5.difference(s1,s2) returns elements that are in s1 but not in s2 Pre:
 * two integer arrays s1 and s2 Post: random double number D generated,
 * where min < D < max
 *
 */
public static final int NUMBER_ADDED = 0;
public static final int NUMBER_ALREADY_IN_SET = 1;
public static final int NUMBER_REMOVED = 2;
public static final int NUMBER_NOT_IN_SET = 3;
private ArrayList<Integer> al = new ArrayList<Integer>();//creates new array list

IntSet(ArrayList<Integer> source) {
    al.addAll(source);
}

/**
 * Adds an element to a set see top for specifications
 *
 * @param elem element to be added
 * @return NUMBER_ADDED if valid, NUMBER_ALREADY_IN_SET if not
 */
public int addElement(int elem) {
    if (!al.contains(elem)) {
        return NUMBER_ALREADY_IN_SET;
    }
    al.add(elem);
    return NUMBER_ADDED;
}

/**
 * Removes the element from the set See top for specification
 *
 * @param elem element to be removed
 * @return NUMBER_REMOVED if valid, NUMBER_NOT_IN_SET if not
 */
public int removeElement(int elem) {

    if (!al.contains(elem)) {
        return NUMBER_NOT_IN_SET;
    }
    al.remove(elem);
    return NUMBER_ADDED;
}

/**
 * Finds the intersection of two arrays see top for ADT specification
 *
 * @param s1 first array
 * @param s2 second array
 * @return s3 combined array
 */
public IntSet intersection(Integer[] s1, Integer[] s2) {
    Arrays.sort(s1);//sort arrays
    Arrays.sort(s2);
    IntSet s3;
    ArrayList<Integer> intersect = new ArrayList<Integer>();
    int s1index = 0;//initalise index and counters
    int s2index = 0;

    while (s1index < s1.length && s2index < s2.length) {
        if (s1[s1index] == s2[s2index]) {//if present in both arrays
            al.add(s1[s1index]);// add element to s3
            s1index++;//increment
            s2index++;
        } else if (s1[s1index] < s2[s2index]) {//increment the smaller element
            s1index++;
        } else {
            s2index++;
        }
    }
    s3 = new IntSet(al);
    return s3;

}

/**
 * Finds the union of two arrays see top for ADT specification
 *
 * @param s1 first array
 * @param s2 second array
 * @return united array (s4)
 */
public IntSet union(Integer[] s1, Integer[] s2) {
    Arrays.sort(s1);//sort arrays
    Arrays.sort(s2);
    IntSet s3;//initialise arrays
    ArrayList<Integer> union = new ArrayList<Integer>();//creates new array list
    int counter = 0;


    for (int i = 0; i < s1.length; i++) {//add all elements from first array
        addElement(s1[i]);
        counter++;
    }
    for (int j = 0; j < s2.length; j++) {//check second array...
        boolean contains = false;
        for (int i = 0; i < s1.length; i++) { //...with first 
            if (union.contains(s2[j])) { //if you have found a match   
                contains = true; //flag it  
                break; //and break  
            }
        } //end i  
        //if a match has not been found, print out the value  
        if (!contains) {
            addElement(s2[j]);
            counter++;
        }


    }//end j  
    s3 = new IntSet(al);
    return s3;
}//end method 

/**
 * Returns elements of first array not present in the second See top for ADT
 * specification
 *
 * @param s1 first array
 * @param s2 second array
 * @return difference array
 */
public IntSet difference(Integer[] s1, Integer[] s2) {

    IntSet result = intersection(s1, s2);//get intersection array
    IntSet s3;
    Arrays.sort(s1);//sort arrays
    ArrayList<Integer> difference = new ArrayList<Integer>();//creates new array list
    for(int i = 0; i < s1.length; i++)
    {
        difference.add(s1[i]);
    }
    for (int i = 0; i < s1.length; i++) {
        if (!difference.contains(result)) {//if not present in second array
            removeElement(s1[i]);//add value to output array
        }
    }
    s3 = new IntSet(al);
    return s3;
}

}
公共类IntSet{
/**
*@param指定命令行参数
*/
/*
*规格:1.addElement(elem)在集合Pre中添加一个新元素:
*整数元素!=集合中的任何其他元素:添加的编号\u返回
*如果不存在,则数字\u已存在于\u集合中(如果存在)。
*
*2.removeElement(elem)从集合Pre:integer elem中删除一个元素
*已处于设置状态:如果存在,则返回已删除的编号,
*如果不存在,则编号不在集合中。
*
*3.交集(s1,s2)返回两个集合的交集Pre:two
*整数数组s1和s2 Post:包含与
*这两个集合,如果没有共同点,则为空集合
*
*4.并集(s1,s2)返回两个集合的并集:两个整数数组s1
*和s2 Post:两个数组中都存在非重复元素的数组。
*
*5.差分(s1,s2)返回s1中但不在s2中的元素:
*两个整数数组s1和s2 Post:生成随机双数D,
*其中min
假设
al
ArrayList
,您可以尝试以下操作:

IntSet[] t = al.toArray(new IntSet[al.size()]);
s3 = Arrays.copyOf(t, t.length);
但是
t
已经是一个副本,所以您可以

return al.toArray(new IntSet[al.size()]);

编辑:查看后
return new IntSet(al, new Integer[al.size()]);  // based on constructor you
                                                // provided in IntSet class
public IntSet intersection(Integer[] s1, Integer[] s2)
IntSet s3 = new IntSet(al);
IntSet(ArrayList<Integer> source) {
    al.addAll(source);
}
public int addElement(int elem) {
   if (!a1.contains(elem)) return  NUMBER_ALREADY_IN_SET;
   a1.add(elem);
   return NUMBER_ADDED;
}