C# 数组索引是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/794760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Index of Array Exist
提问by splatto
I've inherited some code at work that has a really bad smell. I'm hoping to find the most painless solution possible.
我在工作中继承了一些非常难闻的代码。我希望找到最无痛的解决方案。
Is there a way to check if some arbitrary number is a valid element in an array?
有没有办法检查某个任意数字是否是数组中的有效元素?
Example - I need to check if array[25] exists.
示例 - 我需要检查 array[25] 是否存在。
Preferably I would prefer to do this without doing a foreach() through the array to found the rows.
最好我更愿意这样做而不通过数组执行 foreach() 来找到行。
Is there any way to do this, or am I stuck with foreach loop?
有什么办法可以做到这一点,还是我坚持使用 foreach 循环?
采纳答案by Eoin Campbell
Test the length
测试长度
int index = 25;
if(index < array.Length)
{
//it exists
}
回答by Martin Harris
Assuming you also want to check if the item is not null
假设您还想检查该项目是否为空
if (array.Length > 25 && array[25] != null)
{
//it exists
}
回答by Jon Skeet
What exactly do you mean by "is a valid element"? You could just do:
“是有效元素”究竟是什么意思?你可以这样做:
if (array.Length >= 26)
which would tell you whether 25 is a valid index into the array or not (assuming a 0 lower bound).
这将告诉您 25 是否是数组的有效索引(假设下限为 0)。
If you need to know whether it's non-null or not, just use:
如果您需要知道它是否为非空,只需使用:
if (array[25] != null)
(or a combination of the two).
(或两者的结合)。
If these don't help, please give a more precise meaning of "valid" for your problem.
如果这些没有帮助,请为您的问题给出更准确的“有效”含义。
回答by jean
You can use the length of the array, and see if your arbitrary number fits in that range. For example, if you have an array of size 10, then array[25] isn't valid because 25 is not less than 10.
您可以使用数组的长度,并查看您的任意数字是否适合该范围。例如,如果您有一个大小为 10 的数组,则 array[25] 无效,因为 25 不小于 10。
回答by Jhonny D. Cano -Leftware-
You can rather use a List, so you can check the existence.
您可以使用 List,这样您就可以检查是否存在。
List<int> l = new List<int>();
l.Add(45);
...
...
if (l.Count == 25) {
doStuff();
}
int num = 45;
if (l.Contains(num)) {
doMoreStuff();
}
回答by Mike Miller
array.length
will tell you how many elements are in an array
array.length
会告诉你一个数组中有多少个元素
回答by JB King
You could check if the index is less than the length of the array. This doesn't check for nulls or other odd cases where the index can be assigned a value but hasn't been given one explicitly.
您可以检查索引是否小于数组的长度。这不会检查空值或其他奇怪的情况,在这些情况下,可以为索引分配一个值但尚未明确指定一个值。
回答by Colin Desmond
You can check the length of the array to see if item 25 is valid in the sense of being in the array, then you could use
您可以检查数组的长度以查看第 25 项在数组中是否有效,然后您可以使用
if (array.Length > 25)
{
if (array[25] != null)
{
//good
}
}
to see if the array item itself has been set.
查看数组项本身是否已设置。
回答by Wedge
It sounds very much like you're using an array to store different fields. This is definitely a code smell. I'd avoid using arrays as much as possible as they're generally not suitable (or needed) in high-level code.
这听起来很像您使用数组来存储不同的字段。这绝对是一种代码异味。我会尽可能避免使用数组,因为它们通常不适合(或不需要)在高级代码中。
Switching to a simple Dictionary may be a workable option in the short term. As would using a big property bag class. There are lots of options. The problem you have now is just a symptom of bad design, you should look at fixing the underlying problem rather than just patching the bad design so it kinda, sorta mostly works, for now.
短期内切换到简单的字典可能是一个可行的选择。就像使用大属性包类一样。有很多选择。你现在遇到的问题只是糟糕设计的一个症状,你应该着眼于修复潜在的问题,而不是仅仅修补糟糕的设计,所以它现在有点,几乎可以正常工作。
回答by theJerm
// I'd modify this slightly to be more resilient to a bad parameter
// it will handle your case and better handle other cases given to it:
int index = 25;
if (index >= 0 && index < array.Length)
{
// Array element found
}