计数在数组occurances数数组、occurances

2023-09-08 11:41:09 作者:戀愛丶只不過是青春的炒作

我希望计算在ActionScript 3.0阵列occurances的数量。说我有

  VAR项目:数组= [苹果,橙子,葡萄,橙子,苹果,葡萄];
 

我怎样才能使它显示字符串匹配的数量?举例来说,结果是:苹果= 2,橘子= 2等。

我从另一个类似的问题,这code:

 私有函数getCount将(fruitArray:阵列,fruitName:字符串):INT {
    VAR计数:= 0;
    对于(VAR我:= 0; I< fruitArray.length;我++){
        如果(fruitArray [I] .toLowerCase()== fruitName.toLowerCase()){
            算上++;
        }
    }
    返回计数;
}

VAR水果:数组= [苹果,橙子,葡萄,橙子,苹果,葡萄];
VAR appleCount = getCount将(水果,苹果); //返回2
VAR grapeCount = getCount将(水果,葡萄); //返回2
VAR orangeCount = getCount将(水果,橘子); //返回2
 

在此code,如果你想获得说苹果的计数。您需要设置的变量为每个项目(VAR appleCount = getCount将(水果,苹果))。但是,如果你有水果名字成千上万,这是不可能写下的每一个水果新的变量。

我是完全新的AS3所以请原谅我。请在您的code明确的意见,因为我想了解code。

解决方案

  VAR项目:数组= [苹果,橙子,葡萄,橙子,苹果,葡萄];

    //写出现每串的计数到地图{fruitName:数}
    VAR的水果:字符串;
    变种地图:对象= {}; //创建空的对象,将举行计数器的值,每个水果,例如地图[苹果]将保持计数器苹果

    //遍历为阵列中的每个字符串,并增加发生计数器此字符串被1
    每个(水果项)
    {
        //水果的名字第一次遇到,分配计数器1
        如果(!图[水果])
            地图[水果] = 1;
        //水果名称旁边的遭遇,只是增加了1计数器
        其他
            图[水果] ++;
    }

    //迭代的地图属性追查的结果
    对于(水果图)
    {
        跟踪(水果,=,图[水果]);
    }
 
拜托,面试别再问我计数排序了

输出:

 苹果= 2
葡萄= 2
橘子= 2
 

I want to count number of occurances in an array in ActionScript 3.0. Say I have

var item:Array = ["apples", "oranges", "grapes", "oranges", "apples", "grapes"];

How can I make it show the number of matching strings? For instance, result: apples = 2, oranges = 2 etc.

I got this code from another similar question:

    private function getCount(fruitArray:Array, fruitName:String):int {
    var count:int=0;
    for (var i:int=0; i<fruitArray.length; i++) {
        if(fruitArray[i].toLowerCase()==fruitName.toLowerCase()) {
            count++;
        }
    }
    return count;
}

var fruit:Array = ["apples", "oranges", "grapes", "oranges", "apples", "grapes"];
var appleCount=getCount(fruit, "apples"); //returns 2
var grapeCount=getCount(fruit, "grapes"); //returns 2
var orangeCount=getCount(fruit, "oranges"); //returns 2

In this code, if you want to get the count of say "apple". You need to set up variables for each item (var appleCount=getCount(fruit, "apples")). But what if you have hundreds and thousands of fruit names, it is not possible to write down new variables for each and every fruit.

I'm completely new to AS3 so forgive me. Please include clear comments in your code as I want to understand the code.

解决方案

    var item:Array = ["apples", "oranges", "grapes", "oranges", "apples", "grapes"];

    //write the count number of occurrences of each string into the map {fruitName:count}
    var fruit:String;
    var map:Object = {}; //create the empty object, that will hold the values of counters for each fruit, for example map["apples"] will holds the counter for "apples"

    //iterate for each string in the array, and increase occurrence counter for this string by 1 
    for each(fruit in item)
    {
        //first encounter of fruit name, assign counter to 1
        if(!map[fruit])
            map[fruit] = 1;
        //next encounter of fruit name, just increment the counter by 1
        else
            map[fruit]++;
    }

    //iterate by the map properties to trace the results 
    for(fruit in map)
    {
        trace(fruit, "=", map[fruit]);
    }

output:

apples = 2
grapes = 2
oranges = 2