阵列索引是否存在 [英] Does Index of Array Exist

查看:123
本文介绍了阵列索引是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作中继承了一些非常难闻的代码。我希望找到最无痛的解决方案。

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?

示例 - 我需要检查数组[25]是否存在。

Example - I need to check if array[25] exists.

我更愿意这样做而不做一个foreach( )通过数组找到行。

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?

推荐答案

测试长度

int index = 25;
if(index < array.length)
{
    //it exists
}

这篇关于阵列索引是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆