gpt4 book ai didi

java - 错误: incompatible types (for sets)

转载 作者:行者123 更新时间:2023-12-02 06:31:36 30 4
gpt4 key购买 nike

有类型转换套装之类的东西吗?

我有一个构造函数,它需要 Set<String> things作为参数,我想设置一个字段 TreeSet<String> stuff成为这最初的一组事物。但是,我不断收到错误。 Java 不喜欢我的声明

stuff = things;

所以我想知道将事物的所有元素放入列表中,然后将该列表的元素移动到事物中是否是一个好的解决方案,或者是否有更好的方法。

这是我想到的:

public class Anagrams {
private TreeSet<String> allWords;

//pre: the given dictionary must be in alphabetical order, if null throws an
// illegalArgumentException
//post: creates a new anagram solver
public Anagrams(Set<String> dictionary) {
if(dictionary == null) {
throw new IllegalArgumentException();
}
for(String word : dictionary){
String temp = word;
allWords.add(word);
}
}

最佳答案

实例化一个新的TreeSet<String>通过从 things 填充它:

stuff = new TreeSet<String>(things);

关于java - 错误: incompatible types (for sets),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013250/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com