0
for(var i:int=0;i<stringArray.length;i++)
{
    if(stringArray[i]==""||stringArray[i]==null)
    {
        trace("Element at "+i+" found empty");
    }
}

我在 AS3 中有一个字符串数组。现在我想检查索引处的特定元素i是否未设置,我该怎么做?

当未设置的字符串到达​​时,我使用上述代码得到的错误如下 -

RangeError: Error #1125: The index 2 is out of range 2.
4

4 回答 4

5

由于空字符串 andnullundefined都是falsy,在条件语句中被视为 boolean false ,并且您说您的数组将只包含字符串,您应该可以这样检查:

for(var i:int=0;i<stringArray.length;i++)
{
    if(!stringArray[i])
    {
        trace("Element at "+i+" found empty");
    }
} 
于 2013-02-27T20:26:21.850 回答
1

检查是否相等undefined以查看是否未设置某个索引。

于 2013-02-27T19:22:26.717 回答
0

也许只是添加一个边界条件来检查传递的数组索引是否在范围内,例如:

function isValidStringInArray(index:int,array:Array):Boolean{
   if(index >= 0 && index < array.length) return ((array[index] != null || array[index] != undefined || array[index].length > 0);
   else return false;
}
于 2013-02-27T19:22:34.733 回答
0

As it turns out, the error had nothing to do with the code I provided, it was an error with bitmap materials in away3d library, which is completely different from the question I asked. But good answers anyway.

Thanks to Lars comment, I thought about other possibilities of errors too, and found it.

于 2013-02-27T20:54:12.807 回答