清除数组并分配新元素

时间:2021-09-25 13:42:07

I have the following code:

我有以下代码:

if ((mySo.data.currentIds != null ) &&
    (mySo.data.currentIds.length > 0))
    currentIds = mySo.data.currentIds.split(',');

Previously, currentIds was initialized with this code:

以前,使用以下代码初始化currentIds:

currentIds = new Array(-1, -1, -1);

I want to ask if this is the best way to replace elements with new ones in an array:

我想问一下,这是否是用数组中的新元素替换元素的最佳方法:

currentIds = mySo.data.currentIds.split(',');

I'm talking about memory leaks and performance. I don't know if previous instance of `currentIds' is cleared from memory or not.

我在谈论内存泄漏和性能。我不知道是否从内存中清除了先前的“currentIds”实例。

Maybe, I can do this instead:

也许,我可以这样做:

currentIds.splice(0, 3, mySo.data.currentIds.split(','));

Any suggestions?

3 个解决方案

#1


2  

If the previous array is not referenced anywhere else then it will be marked for garbage collection. That doesn't mean that it will be destroyed immediately, but that Flash can and will destroy it when it needs to.

如果之前的数组未在其他任何地方引用,那么它将被标记为垃圾回收。这并不意味着它会被立即销毁,但Flash可以并且会在需要时将其销毁。

#2


2  

Your splice syntax is bad.

你的拼接语法很糟糕。

With the splice, what you are doing is putting mySo.data.currentIds.split(',') into the first index of currentIds:

使用拼接,你正在做的是将mySo.data.currentIds.split(',')放入currentIds的第一个索引中:

var currentIds:Array = [-1, -1, -1];
currentIds.splice(0, 3, [1,2,3]);
//The entire array is in index 0:
trace(currentIds.length); //1
trace(currentIds[0]);     //1,2,3

However, depending on what you intend, a splice type of thing could be better. For example, if you reference the currentIds with another variable somewhere else, new Array. That reference will continue to point to the old array.

但是,根据您的意图,拼接类型的东西可能会更好。例如,如果您使用另一个其他变量引用currentIds,则为新数组。该引用将继续指向旧数组。

eg.

var currentIds:Array = [-1, -1, -1];
var currentIdsReference:Array = currentIds;
currentIds = [1,2,3];
trace(currentIdsReference); //-1,-1,-1

vs

var currentIds:Array = [-1, -1, -1];
var currentIdsReference:Array = currentIds;
currentIds.length = 0;
currentIds.push.apply(null, [1,2,3]);
trace(currentIdsReference); //1,2,3

#3


0  

It would be helpful if we knew what you're trying to accomplish; specifically if you're trying to create arrays within arrays (I don't think so, but it's hard to tell without any idea of what each piece is for).

如果我们知道你想要完成什么,那将会有所帮助;特别是如果你试图在数组中创建数组(我不这么认为,但很难分辨出每个部分的用途)。

Also, you didn't let us know what variable types you're working with. .split() is a String function, not an Array function.

另外,您没有告诉我们您正在使用的变量类型。 .split()是一个String函数,而不是一个Array函数。

As for garbage collection, if you replace the existing array with a new one (via whatever method) then the old array should be collected and disposed of eventually -- as long as you don't have any other references to it.

对于垃圾收集,如果用新的数组替换现有数组(通过任何方法),那么旧数组应该被收集并最终处理掉 - 只要你没有任何其他引用它。

Finally, I'd point out that you might have an encapsulation issue -- not strictly a code error, but possibly indicating poor design. You show that you're accessing mySo.data.currentIds. What is mySo and data? And why are you reaching down into them? That's potentially adding a lot of coupling. You might want to refactor to do something like: mySo.resetIds(); instead.

最后,我要指出你可能有一个封装问题 - 严格来说不是代码错误,但可能表明设计不佳。您显示您正在访问mySo.data.currentIds。什么是mySo和数据?你为什么要深入他们?这可能会增加很多耦合。您可能希望重构以执行以下操作:mySo.resetIds();代替。

#1


2  

If the previous array is not referenced anywhere else then it will be marked for garbage collection. That doesn't mean that it will be destroyed immediately, but that Flash can and will destroy it when it needs to.

如果之前的数组未在其他任何地方引用,那么它将被标记为垃圾回收。这并不意味着它会被立即销毁,但Flash可以并且会在需要时将其销毁。

#2


2  

Your splice syntax is bad.

你的拼接语法很糟糕。

With the splice, what you are doing is putting mySo.data.currentIds.split(',') into the first index of currentIds:

使用拼接,你正在做的是将mySo.data.currentIds.split(',')放入currentIds的第一个索引中:

var currentIds:Array = [-1, -1, -1];
currentIds.splice(0, 3, [1,2,3]);
//The entire array is in index 0:
trace(currentIds.length); //1
trace(currentIds[0]);     //1,2,3

However, depending on what you intend, a splice type of thing could be better. For example, if you reference the currentIds with another variable somewhere else, new Array. That reference will continue to point to the old array.

但是,根据您的意图,拼接类型的东西可能会更好。例如,如果您使用另一个其他变量引用currentIds,则为新数组。该引用将继续指向旧数组。

eg.

var currentIds:Array = [-1, -1, -1];
var currentIdsReference:Array = currentIds;
currentIds = [1,2,3];
trace(currentIdsReference); //-1,-1,-1

vs

var currentIds:Array = [-1, -1, -1];
var currentIdsReference:Array = currentIds;
currentIds.length = 0;
currentIds.push.apply(null, [1,2,3]);
trace(currentIdsReference); //1,2,3

#3


0  

It would be helpful if we knew what you're trying to accomplish; specifically if you're trying to create arrays within arrays (I don't think so, but it's hard to tell without any idea of what each piece is for).

如果我们知道你想要完成什么,那将会有所帮助;特别是如果你试图在数组中创建数组(我不这么认为,但很难分辨出每个部分的用途)。

Also, you didn't let us know what variable types you're working with. .split() is a String function, not an Array function.

另外,您没有告诉我们您正在使用的变量类型。 .split()是一个String函数,而不是一个Array函数。

As for garbage collection, if you replace the existing array with a new one (via whatever method) then the old array should be collected and disposed of eventually -- as long as you don't have any other references to it.

对于垃圾收集,如果用新的数组替换现有数组(通过任何方法),那么旧数组应该被收集并最终处理掉 - 只要你没有任何其他引用它。

Finally, I'd point out that you might have an encapsulation issue -- not strictly a code error, but possibly indicating poor design. You show that you're accessing mySo.data.currentIds. What is mySo and data? And why are you reaching down into them? That's potentially adding a lot of coupling. You might want to refactor to do something like: mySo.resetIds(); instead.

最后,我要指出你可能有一个封装问题 - 严格来说不是代码错误,但可能表明设计不佳。您显示您正在访问mySo.data.currentIds。什么是mySo和数据?你为什么要深入他们?这可能会增加很多耦合。您可能希望重构以执行以下操作:mySo.resetIds();代替。