从 LinkedHashSet 到 String 的 ClassCastException

ClassCastException from LinkedHashSet to String

我想从 LinkedHashSet 中获取字符串数组,但在我尝试在 foreach 循环中打印我的值的行中出现 ClassCastException。

我知道 powerset 方法有问题,但是当我试图通过在方法中添加 LinkedHashSet 来修复它时,我失败了。

我相信有一种方法可以从对象中获取字符串。最后的想法是将它写在文件中,而不是用正则表达式来处理,但它看起来很奇特......

追踪: 线程 "main" java.lang.ClassCastException 中的异常:java.util.LinkedHashSet 无法转换为 [Ljava.lang.String; 在 Main.main(Main.java:61)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.xml.stream.events.Characters;

public class Main {
     public static void main(String[] args) {

           String set[] = {"a", "b", "c", "d", "e"};
           char rset[] = new char[5];
           String sSet[] = null;
           String s1 = "";
           String s2 = "";
           ArrayList<char[]> arr = new ArrayList();
           arr.add(new char[]{'b','c','d'});
           arr.add(new char[]{'e','a', 'b'});
           arr.add(new char[]{'c','a'});
           arr.add(new char[]{'b','d','c'});
           arr.add(new char[]{'b','d','c'});

           int i=0;
           String buffer = "";
           for (char[] strings : arr) {
               System.out.println(Arrays.toString(strings));
               if(buffer.indexOf(strings[(strings.length)-1]) < 0){
                   buffer = buffer + strings[(strings.length)-1];
               }
               i++;
           }
           rset = buffer.toCharArray();
           Arrays.sort(rset);

           for (String ch : set) {
            s1+=ch + " ";
           }
           for (char ch : rset) {
                s2+=ch + " ";
            }

           System.out.println(s1);
           System.out.println(s2);
           String diff = difference(s1, s2);
           System.out.println(diff);

           //form the power set
           LinkedHashSet myPowerSet = powerset(set);
           //display the power set


           System.out.println(myPowerSet.toString());


           ArrayList<String[]> sArr = new ArrayList(myPowerSet);

           for (String[] strings : sArr) {
               System.out.println(strings);
           }
       }

    private static String difference(String s1, String s2) {
        String diff = "";
        String[] strList1 = s1.split(" ");
           String[] strList2 = s2.split(" ");

           List<String> list1 = Arrays.asList(strList1);
           List<String> list2 = Arrays.asList(strList2);

           // Prepare a union
           List<String> union = new ArrayList<>(list1);
           union.addAll(list2);

           // Prepare an intersection
           List<String> intersection = new ArrayList<>(list1);
           intersection.retainAll(list2);

           // Subtract the intersection from the union
           union.removeAll(intersection);

           for (String s : union) {
               //System.out.println(s);
               diff += s;
           }
           return diff;
    }

     private static LinkedHashSet powerset(String[] set) {

           //create the empty power set
           LinkedHashSet power = new LinkedHashSet();

           //get the number of elements in the set
           int elements = set.length;

           //the number of members of a power set is 2^n
           int powerElements = (int) Math.pow(2,elements);

           //run a binary counter for the number of power elements
           for (int i = 0; i < powerElements; i++) {

               //convert the binary number to a string containing n digits
               String binary = intToBinary(i, elements);

               //create a new set
               LinkedHashSet innerSet = new LinkedHashSet();

               //convert each digit in the current binary number to the corresponding element
                //in the given set
               for (int j = 0; j < binary.length(); j++) {
                   if (binary.charAt(j) == '1')
                       innerSet.add(set[j]);
               }

               //add the new set to the power set
               power.add(innerSet);

           }
           return power;
       }
       /**
         * Converts the given integer to a String representing a binary number
         * with the specified number of digits
         * For example when using 4 digits the binary 1 is 0001
         * @param binary int
         * @param digits int
         * @return String
         */
       private static String intToBinary(int binary, int digits) {

           String temp = Integer.toBinaryString(binary);
           int foundDigits = temp.length();
           String returner = temp;
           for (int i = foundDigits; i < digits; i++) {
               returner = "0" + returner;
           }
           return returner;
       } 
}

TL;DR

使用泛型


LinkedHashSet myPowerSet;

其实应该是

LinkedHashSet<LinkedHashSet<String>> myPowerSet;

对代码进行必要的更改,答案就会跃然眼前。

ArrayList<LinkedHashSet<String>> sArr = new ArrayList<>(myPowerSet);

实际上并没有采用 String[]String 因为它是 myPowerSet 中使用的类型所以下面的内容也会被更改

for (Set strings : sArr) {
    System.out.println(strings);
}

完整代码

public static void main(String[] args) {

    String set[] = {"a", "b", "c", "d", "e"};
    char rset[] = new char[5];
    String sSet[] = null;
    String s1 = "";
    String s2 = "";
    ArrayList<char[]> arr = new ArrayList<char[]>();
    arr.add(new char[]{'b','c','d'});
    arr.add(new char[]{'e','a', 'b'});
    arr.add(new char[]{'c','a'});
    arr.add(new char[]{'b','d','c'});
    arr.add(new char[]{'b','d','c'});

    int i=0;
    String buffer = "";
    for (char[] strings : arr) {
        System.out.println(Arrays.toString(strings));
        if(buffer.indexOf(strings[(strings.length)-1]) < 0){
            buffer = buffer + strings[(strings.length)-1];
        }
        i++;
    }
    rset = buffer.toCharArray();
    Arrays.sort(rset);

    for (String ch : set) {
        s1+=ch + " ";
    }
    for (char ch : rset) {
        s2+=ch + " ";
    }

    System.out.println(s1);
    System.out.println(s2);
    String diff = difference(s1, s2);
    System.out.println(diff);

    //form the power set
    LinkedHashSet<LinkedHashSet<String>> myPowerSet = powerset(set);
    //display the power set


    System.out.println(myPowerSet.toString());


    ArrayList<LinkedHashSet<String>> sArr = new ArrayList<>(myPowerSet);

    for (Set strings : sArr) {
        System.out.println(strings);
    }
}

private static String difference(String s1, String s2) {
    String diff = "";
    String[] strList1 = s1.split(" ");
    String[] strList2 = s2.split(" ");

    List<String> list1 = Arrays.asList(strList1);
    List<String> list2 = Arrays.asList(strList2);

    // Prepare a union
    List<String> union = new ArrayList<>(list1);
    union.addAll(list2);

    // Prepare an intersection
    List<String> intersection = new ArrayList<>(list1);
    intersection.retainAll(list2);

    // Subtract the intersection from the union
    union.removeAll(intersection);

    for (String s : union) {
        //System.out.println(s);
        diff += s;
    }
    return diff;
}

private static LinkedHashSet<LinkedHashSet<String>> powerset(String[] set) {

    //create the empty power set
    LinkedHashSet<LinkedHashSet<String>> power = new LinkedHashSet<>();

    //get the number of elements in the set
    int elements = set.length;

    //the number of members of a power set is 2^n
    int powerElements = (int) Math.pow(2,elements);

    //run a binary counter for the number of power elements
    for (int i = 0; i < powerElements; i++) {

        //convert the binary number to a string containing n digits
        String binary = intToBinary(i, elements);

        //create a new set
        LinkedHashSet<String> innerSet = new LinkedHashSet<String>();

        //convert each digit in the current binary number to the corresponding element
        //in the given set
        for (int j = 0; j < binary.length(); j++) {
            if (binary.charAt(j) == '1')
                innerSet.add(set[j]);
        }

        //add the new set to the power set
        power.add(innerSet);

    }
    return power;
}
/**
 * Converts the given integer to a String representing a binary number
 * with the specified number of digits
 * For example when using 4 digits the binary 1 is 0001
 * @param binary int
 * @param digits int
 * @return String
 */
private static String intToBinary(int binary, int digits) {

    String temp = Integer.toBinaryString(binary);
    int foundDigits = temp.length();
    String returner = temp;
    for (int i = foundDigits; i < digits; i++) {
        returner = "0" + returner;
    }
    return returner;
} 

输出

[b, c, d]
[e, a, b]
[c, a]
[b, d, c]
[b, d, c]
a b c d e 
a b c d 
e
[[], [e], [d], [d, e], [c], [c, e], [c, d], [c, d, e], [b], [b, e], [b, d], [b, d, e], [b, c], [b, c, e], [b, c, d], [b, c, d, e], [a], [a, e], [a, d], [a, d, e], [a, c], [a, c, e], [a, c, d], [a, c, d, e], [a, b], [a, b, e], [a, b, d], [a, b, d, e], [a, b, c], [a, b, c, e], [a, b, c, d], [a, b, c, d, e]]
[]
[e]
[d]
[d, e]
[c]
[c, e]
[c, d]
[c, d, e]
[b]
[b, e]
[b, d]
[b, d, e]
[b, c]
[b, c, e]
[b, c, d]
[b, c, d, e]
[a]
[a, e]
[a, d]
[a, d, e]
[a, c]
[a, c, e]
[a, c, d]
[a, c, d, e]
[a, b]
[a, b, e]
[a, b, d]
[a, b, d, e]
[a, b, c]
[a, b, c, e]
[a, b, c, d]
[a, b, c, d, e]

为了帮助查找代码中的缺陷,在整个代码中使用泛型确实很有帮助。当您忘记使用泛型时,像 Eclipse 这样的 IDE 会警告您。

采用 powerset 方法并使用泛型产生以下代码:

private static Set<Set<String>> powerset(String[] set) {

    // create the empty power set
    Set<Set<String>> power = new LinkedHashSet<>();

    // get the number of elements in the set
    int elements = set.length;

    // the number of members of a power set is 2^n
    int powerElements = (int) Math.pow(2, elements);

    // run a binary counter for the number of power elements
    for (int i = 0; i < powerElements; i++) {

        // convert the binary number to a string containing n digits
        String binary = intToBinary(i, elements);

        // create a new set
        Set<String> innerSet = new LinkedHashSet<String>();

        // convert each digit in the current binary number to the corresponding element
        // in the given set
        for (int j = 0; j < binary.length(); j++) {
            if (binary.charAt(j) == '1')
                innerSet.add(set[j]);
        }

        // add the new set to the power set
        power.add(innerSet);

    }
    return power;
}

所以您返回的实际对象是一组集合。 由于您没有使用泛型,您可以欺骗自己(和编译器)相信您使用的是 Set。在运行时它仍然是一个 Set> 因此抛出 ClassCastException。

打印结果的正确方法如下:

    for (Set<String> setOfStrings : myPowerSet) {
        System.out.println(setOfStrings);
    }