This is my code and I cant seem to get the difference method to work right I get an error when I try to see if !other.contains(set) and the same when I try to add it after checking if it contains it. Not sure what I need to do to fix it but if someone could help that would be excellent.
这是我的代码,我似乎无法让差异方法正常工作当我尝试查看是否!other.contains(set)时我得到一个错误,当我在检查它是否包含它之后尝试添加它时也是如此。不知道我需要做些什么来修复它,但如果有人可以提供帮助,那将是非常好的。
private ArrayList<String> set = new ArrayList<String>();
public boolean add(String word)
{
boolean x = true;
if (word == null || word == "")
{
x = false;
} else
{
if (word.length() > 0)
{
set.add(word);
x = true;
size++;
}
}
return x;
}
public boolean contains(String word)
{
boolean contains = false;
if (word != null)
contains = set.contains(word);
return contains;
}
/**
* WordSet - This method should return a WordSet containing the words that
*are in the current set but not in the other set (this – other).
*
* @param other
* @return WordSet
*/
public WordSet difference(WordSet other)
{
WordSet differenceSet = new WordSet();
boolean x = false;
if (other != null)
for (String str : set)
if (!other.contains(set))
differenceSet.add(set);
return differenceSet;
}
2 个解决方案
#1
0
You need to check for null condition. if (null != other.contains(set) && !other.contains(set)){ //your code here }
您需要检查空状态。 if(null!= other.contains(set)&&!other.contains(set)){// your code here}
or I think you might want to check str instead of _set_the just replace it in contains()
或者我认为你可能想检查str而不是_set_the只需在contains()中替换它
if (word != null) contains = set.contains(word); return contains; // -> this will return null if contains is null which is causing the second error
Apply similar patch here
if(word!= null)contains = set.contains(word); return contains; // - >如果contains为null,则返回null,导致第二次错误在此处应用类似的补丁
if (word != null) {
if(null != set.contains(word) {
contains = set.contains(word);
}
}
return contains;
if(word!= null){if(null!= set.contains(word){contains = set.contains(word);}} return contains;
#2
-1
Your question is not really clear But I guess below is the issue with your code.
你的问题不是很清楚但我想下面是你的代码的问题。
public WordSet difference(WordSet other)
{
WordSet differenceSet = new WordSet();
boolean x = false;
if (other != null)
for (String str : set)
{
if (!other.contains(str)) // You need to match with string in the set not the set.
differenceSet.add(set);
}
return differenceSet;
}
#1
0
You need to check for null condition. if (null != other.contains(set) && !other.contains(set)){ //your code here }
您需要检查空状态。 if(null!= other.contains(set)&&!other.contains(set)){// your code here}
or I think you might want to check str instead of _set_the just replace it in contains()
或者我认为你可能想检查str而不是_set_the只需在contains()中替换它
if (word != null) contains = set.contains(word); return contains; // -> this will return null if contains is null which is causing the second error
Apply similar patch here
if(word!= null)contains = set.contains(word); return contains; // - >如果contains为null,则返回null,导致第二次错误在此处应用类似的补丁
if (word != null) {
if(null != set.contains(word) {
contains = set.contains(word);
}
}
return contains;
if(word!= null){if(null!= set.contains(word){contains = set.contains(word);}} return contains;
#2
-1
Your question is not really clear But I guess below is the issue with your code.
你的问题不是很清楚但我想下面是你的代码的问题。
public WordSet difference(WordSet other)
{
WordSet differenceSet = new WordSet();
boolean x = false;
if (other != null)
for (String str : set)
{
if (!other.contains(str)) // You need to match with string in the set not the set.
differenceSet.add(set);
}
return differenceSet;
}