Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在调用数组之前检查数组中是否存在点值_Arrays_Actionscript 3 - Fatal编程技术网

Arrays 在调用数组之前检查数组中是否存在点值

Arrays 在调用数组之前检查数组中是否存在点值,arrays,actionscript-3,Arrays,Actionscript 3,我试图比较两个不同数组中两个点的X值。问题是,有时我正在比较的值可能还不存在,在调用此变量之前,我不完全确定如何进行此检查 我的代码如下所示 if (blueShipPositions[0].x == redShipPositions[0].x) { trace("Match in" + blueShipPositions[0]); } 数组中任意给定点的x值加起来为4,因此我遇到的错误如下所示 if (blueShipPositions[0].x ==

我试图比较两个不同数组中两个点的X值。问题是,有时我正在比较的值可能还不存在,在调用此变量之前,我不完全确定如何进行此检查

我的代码如下所示

if (blueShipPositions[0].x == redShipPositions[0].x) {
            trace("Match in" + blueShipPositions[0]);
        }
数组中任意给定点的x值加起来为4,因此我遇到的错误如下所示

if (blueShipPositions[0].x == redShipPositions[4].x) {
            trace("true");
        }
如果redShipPositions[4].x不存在,我会得到一个错误。
我知道IndexOf函数,但我不知道如何在这里应用它。

将其包装在确定数组大小的IF语句中:

if (redShipPositions.length >= 5) {
     if (blueShipPositions[0].x == redShipPositions[4].x) {
               trace("true");
     }
}
另一种方法是使用int局部变量来指示数组的大小:

myArraySize = redShipPositions.length;

然后,您可以使用它作为指示器/(停止点)安全地执行代码,而不必担心IndexAutofBound异常。

作为您问题的答案,以以下示例为例:(内部注释)

var数组:数组=新数组(1,5,6,55)
风险值指数:数字=3
//数组元素的类型,在本例中为Number
变量元素类型:*=编号
//因此,为了验证数组[index]是否存在,我们可以使用
if(数组[索引]){
跟踪(数组[索引])
}
//或
if(数组[索引]!=未定义){
跟踪(数组[索引])
}
//或
if(数组[索引]!=null){
跟踪(数组[索引])
}
//或
if(数组的类型[索引]!=“未定义”){
跟踪(数组[索引])
}
//或
if(数组[索引]是元素类型){
跟踪(数组[索引])
}
//或
if(数组[索引]作为元素类型!=null){
跟踪(数组[索引])
}
//或
//这里您必须记住,ActionScript中的数组总是从索引0开始,这就是为什么我们使用<,而不是
var array:Array = new Array(1, 5, 6, 55)
var index:Number = 3

// type of the elements of array, in this case is Number
var element_type:* = Number

// so to verify if the array[index] exists, we can use

if(array[index]) {

    trace(array[index])

}

// or

if(array[index] != undefined) {

    trace(array[index])

}

// or

if(array[index] != null) {

    trace(array[index])

}

// or

if(typeof array[index] != 'undefined') {

    trace(array[index])

}

// or

if(array[index] is element_type) {

    trace(array[index])

}

// or

if(array[index] as element_type != null) {

    trace(array[index])

}

// or

// here you have to put in the mind that arrays in ActionScript starts always from the index 0, thats why we use < and not <= 
if(index < array.length) {

    trace(array[index])

}

// we use array.indexOf to search an element and not an index
if(array.indexOf(55)){

    trace('we have "55" in this array')

} else {

    trace('we don\'t have "55" in this array')

}