遍历 HashSet 时出现 ClassCastException
ClassCastException when iterating over HashSet
我有一个 HashSet<String>
,里面充满了名为 shingles
的字符串(如果你把字符串 "Fizzbuzz" 分解成带状疱疹,你会得到 "Fi", "iz"、"zz"、"zb"、"bu"、"uz"、"zz"。不重要,但这就是瓦片在这种情况下的含义)。
我正在尝试通过以下方法return HashSet 内容的字符串:
public String getShinglesAsString() {
String shingles = "";
for (String shingle : this.shingles) {
shingles += shingle + " ";
}
return shingles;
}
for (String shingle : this.shingles)
抛出 ClassCastException。具体消息为Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.CharSequence
.
我也尝试过使用 Iterator 对象,但我遇到了同样的问题。这是我的代码,用于从提供给构造函数的字符串生成带状疱疹:
// Will generate a new set of shingles based on sentence
public void generateShingles() {
shingles = new HashSet<>();
for (int i=0; i<getSentence().length()-1; i++) {
getShingles().add(getSentence().charAt(i) + getSentence().charAt(i+1));
}
}
编辑:这是 minmal, complete, verifiable example:
public class SentenceWithShingles {
private String sentence;
private Set<String> shingles;
// Constructor
public SentenceWithShingles(String sentence) {
this.sentence = sentence;
generateShingles();
}
// Will generate a new set of shingles based on sentence
public void generateShingles() {
shingles = new HashSet<>();
for (int i=0; i<getSentence().length()-1; i++) {
getShingles().add(getSentence().charAt(i) + getSentence().charAt(i+1));
}
}
public void setSentence(String sentence) {
this.sentence = sentence;
}
public Set getShingles() {
return shingles;
}
public String getShinglesAsString() {
String shingles = "";
for (String shingle : this.shingles) {
shingles += shingle + " ";
}
return shingles;
}
}
错误在这里:
getShingles().add(getSentence().charAt(i) + getSentence().charAt(i+1));
该行将两个字符加在一起。不连接它们。
getShingles().add(getSentence().charAt(i) + "" + getSentence().charAt(i+1));
将强制使用正确的语义。
我有一个 HashSet<String>
,里面充满了名为 shingles
的字符串(如果你把字符串 "Fizzbuzz" 分解成带状疱疹,你会得到 "Fi", "iz"、"zz"、"zb"、"bu"、"uz"、"zz"。不重要,但这就是瓦片在这种情况下的含义)。
我正在尝试通过以下方法return HashSet 内容的字符串:
public String getShinglesAsString() {
String shingles = "";
for (String shingle : this.shingles) {
shingles += shingle + " ";
}
return shingles;
}
for (String shingle : this.shingles)
抛出 ClassCastException。具体消息为Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.CharSequence
.
我也尝试过使用 Iterator 对象,但我遇到了同样的问题。这是我的代码,用于从提供给构造函数的字符串生成带状疱疹:
// Will generate a new set of shingles based on sentence
public void generateShingles() {
shingles = new HashSet<>();
for (int i=0; i<getSentence().length()-1; i++) {
getShingles().add(getSentence().charAt(i) + getSentence().charAt(i+1));
}
}
编辑:这是 minmal, complete, verifiable example:
public class SentenceWithShingles {
private String sentence;
private Set<String> shingles;
// Constructor
public SentenceWithShingles(String sentence) {
this.sentence = sentence;
generateShingles();
}
// Will generate a new set of shingles based on sentence
public void generateShingles() {
shingles = new HashSet<>();
for (int i=0; i<getSentence().length()-1; i++) {
getShingles().add(getSentence().charAt(i) + getSentence().charAt(i+1));
}
}
public void setSentence(String sentence) {
this.sentence = sentence;
}
public Set getShingles() {
return shingles;
}
public String getShinglesAsString() {
String shingles = "";
for (String shingle : this.shingles) {
shingles += shingle + " ";
}
return shingles;
}
}
错误在这里:
getShingles().add(getSentence().charAt(i) + getSentence().charAt(i+1));
该行将两个字符加在一起。不连接它们。
getShingles().add(getSentence().charAt(i) + "" + getSentence().charAt(i+1));
将强制使用正确的语义。