[英]Check if Array Position Exists
I am trying to check if the position of an array exists or not. 我试图检查数组的位置是否存在。
I am trying to output positions 1, 2, 3, 4 and 5 string values from a list. 我试图从列表中输出位置1,2,3,4和5的字符串值。 Where the list is less than 5, it needs to display a '-' as the string value.
如果列表小于5,则需要显示“ - ”作为字符串值。
For example, a list of 3 should display: Value, Value, Value, -, -
例如,应显示3的列表:
Value, Value, Value, -, -
I cannot however work out how to check this, and I keep getting index was out of range errors. 然而,我无法弄清楚如何检查这一点,并且我不断得到索引超出范围错误。
I have tried: 我努力了:
if (String.IsNullOrEmpty(formGuideCount[3]))
{
game4 = formGuideCount[3];
}
else
{
game4 = "-";
}
Can anyone tell me what I should be using to check if that position does not exist? 任何人都可以告诉我应该用什么来检查该位置是否不存在?
Thanks 谢谢
You can use the Array.Length of array to validate the index location exists. 您可以使用Array.Length数组来验证索引位置是否存在。
if(formGuideCount.Length > 3)
{
game4 = formGuideCount[3];
}
You can check yourArray.Length
. 你可以查看你的
yourArray.Length
。
If you are using multi dimensional arrays, you can do this: 如果您使用的是多维数组,则可以执行以下操作:
yourArray.GetLength(0) //first dimension length
yourArray.GetLength(1) //second dimension length
// and so on
Use the a Length
member of the array 使用数组的
Length
成员
if (formGuideCount.Length > 3)
{
game4 = formGuideCount[3];
}
else
{
game4 = "-";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.