I am trying to implement a binary search tree in C#, and following Cormen et al chapter 12 to do it. To do this I need to use nullable types, like this:
我正在尝试在C#中实现二叉搜索树,并遵循Cormen等人的第12章来实现它。为此,我需要使用可空类型,如下所示:
public int insert(Node newNode) // return array index of added node
{
int? y = null;
int? x = this.root;
while (x != null)
{
y = (int)x;
if (newNode.key < this.tree[x])
{ }
}
return 0;
}
Now I get the following error:
现在我收到以下错误:
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast)?
无法隐式转换类型'int?' 'int'。存在显式转换(您是否错过了演员表)?
For this if (newNode.key < this.tree[x])
line.
Is it illegal to use nullable types to acces an array index?
Can I initialize the array maybe in a different way to allow it?
Or should I forget about null and use -1 for instance?
对于这个if(newNode.key
3 个解决方案
#1
You have the x != null
constraint in place, all you need is this.tree[x.Value]
.
你有x!= null约束,你需要的只是this.tree [x.Value]。
And it's not clear what y
is for but I doubt that you need or want that (int)
cast. Also, it matters what the types of tree[x]
and newNode.key
are.
目前尚不清楚y是什么,但我怀疑你需要或想要那个(int)演员。此外,树[x]和newNode.key的类型也很重要。
Is it illegal to use nullable types to acces an array index?
使用可空类型访问数组索引是不合法的吗?
Yes
Can I initialize the array maybe in a different way to allow it?
我可以以不同的方式初始化阵列以允许它吗?
No
Or should I forget about null and use -1 for instance?
或者我应该忘记null并使用-1例如?
That depends. Both are viable when done correctly.
那要看。如果正确完成,两者都是可行的
#2
Cannot implicitly convert type 'int?' to 'int'.
无法隐式转换类型'int?' 'int'。
You are trying to compare an int? to an int. The compiler is essentially saying "what should I do with this comparison if the int? is in fact null. That's a question the compiler cannot work out, so you will have to provide that logic.
你想比较一个int?到一个int。编译器本质上是在说“如果int?实际上是null,我应该怎么做这个比较。这是编译器无法解决的问题,所以你必须提供这个逻辑。
In other words, since you already guarded against x being null, use
换句话说,因为你已经防止x为null,所以使用
this.tree[x.Value]
#3
check the variable if it HasValue
if exist use the Value
检查变量是否为HasValue(如果存在)使用Value
public int insert(Node newNode) // return array index of added node
{
int? y = null;
int? x = this.root;
while (x.HasValue)
{
y = x;
if (newNode.key < this.tree[x.Value])
{
//your logic here
}
}
return 0;
}
#1
You have the x != null
constraint in place, all you need is this.tree[x.Value]
.
你有x!= null约束,你需要的只是this.tree [x.Value]。
And it's not clear what y
is for but I doubt that you need or want that (int)
cast. Also, it matters what the types of tree[x]
and newNode.key
are.
目前尚不清楚y是什么,但我怀疑你需要或想要那个(int)演员。此外,树[x]和newNode.key的类型也很重要。
Is it illegal to use nullable types to acces an array index?
使用可空类型访问数组索引是不合法的吗?
Yes
Can I initialize the array maybe in a different way to allow it?
我可以以不同的方式初始化阵列以允许它吗?
No
Or should I forget about null and use -1 for instance?
或者我应该忘记null并使用-1例如?
That depends. Both are viable when done correctly.
那要看。如果正确完成,两者都是可行的
#2
Cannot implicitly convert type 'int?' to 'int'.
无法隐式转换类型'int?' 'int'。
You are trying to compare an int? to an int. The compiler is essentially saying "what should I do with this comparison if the int? is in fact null. That's a question the compiler cannot work out, so you will have to provide that logic.
你想比较一个int?到一个int。编译器本质上是在说“如果int?实际上是null,我应该怎么做这个比较。这是编译器无法解决的问题,所以你必须提供这个逻辑。
In other words, since you already guarded against x being null, use
换句话说,因为你已经防止x为null,所以使用
this.tree[x.Value]
#3
check the variable if it HasValue
if exist use the Value
检查变量是否为HasValue(如果存在)使用Value
public int insert(Node newNode) // return array index of added node
{
int? y = null;
int? x = this.root;
while (x.HasValue)
{
y = x;
if (newNode.key < this.tree[x.Value])
{
//your logic here
}
}
return 0;
}