我想从 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;
}
}