简体   繁体   English

AS3从阵列中移除元素而无需更改位置

[英]AS3 Removing Elements from Array w/o Changing Position

I want to write a function which removes elements from an array of integers starting from the lowest values without changing the positions of the elements. 我想编写一个函数,该函数从最低值开始的整数数组中删除元素,而无需更改元素的位置。 Programming language is ActionScript3. 编程语言是ActionScript3。

eg (these are individual trace statements) 例如(这些是单独的跟踪语句)

    var aNumArr:Array = [0,7,2,5,9,0]
    trace(RemoveMinValues(aNumArr, 1, false)) //output: 7,2,5,9,0
    //trace(RemoveMinValues(aNumArr, -1, true)) //output: 0,7,2,5,9
    //trace(RemoveMinValues(aNumArr, 2)) //output: 7,2,5,9

I have managed to remove the lowest values in an array using sort(Array.NUMERIC) and sort(Array.DESCENDING) . 我设法使用sort(Array.NUMERIC)sort(Array.DESCENDING)删除了数组中的sort(Array.DESCENDING)

But I can't seem to figure out how to move the elements back to their original positions. 但是我似乎无法弄清楚如何将元素移回其原始位置。

As this is an assignment , I cannot copy the entire function code. 由于这是一项任务 ,因此我无法复制整个功能代码。 And I wish you not to tell me the exact answer, but rather give me insight on how to go about doing it. 我希望您不要告诉我确切的答案,而是让我对如何执行此操作有深入的了解。

Hope I was clear enough. 希望我足够清楚。 Please let me know if you need additional info. 如果您需要其他信息,请告诉我。

Thanks in advance. 提前致谢。


EDIT: I realised I missed out a few things on the function. 编辑:我意识到我错过了功能上的一些东西。 I've also changed the aNumArr and desired output values to make it clearer what I want. 我还更改了aNumArr和所需的输出值,以使其更清楚我想要的内容。

And here was what I did previously: 这是我之前所做的:

    function RemoveMinValues(aNumArr:Array, iMinsToRemove:int):void
    {
         if(iMinsToRemove >= 0)
         {
              aNumArr.sort(Array.NUMERIC);
              for(var i:int = 0; i < iMinsToRemove; ++i)
              {
                   aNumArr.shift();
              }
         }
         else
         {
              aNumArr.sort(Array.DESCENDING);
              for(var i:int = 0; i > iMinsToRemove; --i)
              {
                   aNumArr.pop();
              }
         }
    }

Basically aNumArr:Array is the array of integers specified. 基本上, aNumArr:Array是指定整数的数组。 And iNumbersOfMinsToRemove:int is the number of min values to remove. iNumbersOfMinsToRemove:int是要删除的最小值的数量。 The Assignment requires me to return nothing . 作业要求我不退还任何东西

I know Array.NUMERIC and Array.DESCENDING would change the position of the elements, but I can't seem to figure out the logic on how to keep their positions. 我知道Array.NUMERIC和Array.DESCENDING会更改元素的位置,但是我似乎无法弄清楚如何保持其位置的逻辑。 Please try to keep as simple a possible. 请尽量保持简单。 I'm still an ameture. 我仍然保持乐观。

Instead of removing the value, set it to null . 无需删除该值,而是将其设置为null Better still, set it to Math.NEGATIVE_INFINITY so that the sorting order remains unchanged. 更好的是,将其设置为Math.NEGATIVE_INFINITY ,以使排序顺序保持不变。

That way, the array indices will remain the same because you are modifying the value, not removing it 这样,数组索引将保持不变,因为您正在修改值,而不是删除

First, note that Math.min() and Math.max() can take any number of arguments. 首先,请注意Math.min()和Math.max()可以接受任意数量的参数。 Also, it's important to understand the apply() method available to Function objects. 同样,了解Function对象可用的apply()方法也很重要。 It allows you to pass arguments to the function using an Array. 它允许您使用数组将参数传递给函数。 Let's take advantage of both: 让我们充分利用两者:

var aNumArr:Array = [0,7,2,5,9];
var maxValue:Number = Math.max.apply(null, aNumArr);
var minValue:Number = Math.min.apply(null, aNumArr);

Here's the best part: the "loop" is actually run using native code (inside Flash Player), so it's faster than searching for the minimum or maximum value using a pure ActionScript loop. 最好的部分是:“循环”实际上是使用本机代码运行的(在Flash Player内部),因此它比使用纯ActionScript循环搜索最小值或最大值要快。

[[Edit]] [[编辑]]

Added the code to a spoiler block for others. 将代码添加到其他代码的扰流块中。


If I understand your question correctly, you wish to investigate Array.filter . 如果我正确理解了您的问题,则希望调查Array.filter If the question you are asking requires the final result of each RemoveMinValues pass to return an array that maintains the relative, not absolute, position, then the filter method would work best. 如果您要提出的问题要求每个RemoveMinValues传递的最终结果返回一个保留相对位置(而非绝对位置)的数组,则filter方法将是最佳方法。

Here is the difference given the following array, assuming the second argument to RemoveMinValues is the min value, defaulted to 0. 这是给定以下数组的差异,假设RemoveMinValues的第二个参数是最小值,默认为0。

var arr:Array = [-1,3,2,1,0];

Absolute position: 绝对位置:

RemoveMinValues(arr)    // [null,3,2,1,null]
RemoveMinValues(arr, 1) // [null,3,2,null,null]

Relative position: 相对位置:

RemoveMinValues(arr)    // [3,2,1]
RemoveMinValues(arr, 1) // [3,2]

The following method implementation for RemoveMinValues would provide the "relative" results. RemoveMinValues的以下方法实现将提供“相对”结果。 Please spend some time reading the Array.filter documentation. 请花一些时间阅读Array.filter文档。 Each call to RemoveMinValues, with this specific implementation, returns a new array without modifying the original, as defined in the documentation. 使用此特定实现,对RemoveMinValues的每次调用都会返回一个新数组,而不修改原始数组,如文档中所定义。

Code: 码:

target.filter(function(item:*, ... args):Boolean { return item > minValue }); target.filter(function(item:*,... args):Boolean {返回项目> minValue});

This method will be slower on larger data sets. 在较大的数据集上,此方法将较慢。 Switching to a Vector object would address performance issues. 切换到Vector对象将解决性能问题。

Best of luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM