我希望从LinkedHashSet中获取字符串数组,但我得到了ClassCastException,我尝试在foreach循环中打印我的值。
我知道powerset方法有问题,但是当我尝试通过在方法中添加LinkedHashSet来修复它时,我失败了。
我相信有一种方法可以让Strings摆脱对象。最后的想法是把它写在文件中,而不是与正则表达式拼写,但它似乎异国情调......
跟踪: 线程“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;
}
}
答案 0 :(得分:0)
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]
答案 1 :(得分:0)
为了帮助您找到代码中的缺陷,它有助于在代码中使用泛型。当你忘记使用Generics时,像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;
}
因此,您返回的实际对象是一组集。 由于你没有使用Generics,你可以欺骗自己(和编译器)相信你正在使用Set。在运行时它仍然是一个Set&gt;所以抛出了ClassCastException。
打印结果的正确方法如下:
for (Set<String> setOfStrings : myPowerSet) {
System.out.println(setOfStrings);
}