如何删除重复的名称



考虑以下代码:

下面的代码以这种格式打印processorName:DC5、DR2 DR3但当涉及到复制时,它打印为DR3DR3。我需要删除重复的processorName。我不知道怎么做,我该怎么做?

 public function processorNameFormat(item:Object, column:GridColumn):String
{
    var processorNames:String = "";
    if (!(item is ProcessOrderDO)) {
        return "";
    }
    var poDestReqList:ArrayCollection = item.processOrderDestinationRequirementDOList ;
    for each(var destReq:ProcessOrderDestinationRequirementDO in poDestReqList)
    {
        if(destReq.processorOID == (poDestReqList[poDestReqList.length-1] as ProcessOrderDestinationRequirementDO).processorOID)
            processorNames +=  destReq.processorName ;
        else
            processorNames +=  destReq.processorName+"," ;
    }
    return processorNames;
}

如果您想从结果中删除重复条目,您应该这样做:

public function processorNameFormat(item:Object, column:GridColumn):String
{
    var processorNames:String = "";
    if (!(item is ProcessOrderDO)) {
        return "";
    }
    var d:Dictionary=new Dictionary();
    var poDestReqList:ArrayCollection = item.processOrderDestinationRequirementDOList ;
    for each(var destReq:ProcessOrderDestinationRequirementDO in poDestReqList)
    {
        var s:String=destReq.processorName;
        if (d[s]) continue;
        d[s]=true; // fill the dictionary with values to catch duplicates
        if (processorNames=="")
            processorNames +=  destReq.processorName ;
        else
            processorNames +=  ","+destReq.processorName;
    }
    return processorNames;
}

您创建一个Dictionary,其中包含找到的先前处理器名称,如果在下一个检索到的处理器名称中有匹配,则将跳过它,否则将添加到字典和结果字符串中。

我正在使用更面向对象的方法。

这看起来有点太复杂了:

public function processorNameFormat(item:Object, column:GridColumn):String
{
    if (!(item is ProcessOrderDO)) {
        return "";
}

如果你的函数只能使用特定类型的形参,为什么不简单地将形参声明为该类型呢?

public function processorNameFormat(item:ProcessOrderDO, column:GridColumn):String

这个函数主要操作在形参中传递给它的这个对象。在本例中,将此函数作为方法添加到对象的类ProcessOrderDO:

public function processorNameFormat(column:GridColumn):String

这稍微简化了代码:

public function processorNameFormat(column:GridColumn):String
{
    var processorNames:String = "";
    for each(var destReq:ProcessOrderDestinationRequirementDO in processOrderDestinationRequirementDOList)
    {
        if(destReq.processorOID == (processOrderDestinationRequirementDOList[processOrderDestinationRequirementDOList.length-1] as ProcessOrderDestinationRequirementDO).processorOID)
            processorNames +=  destReq.processorName ;
        else
            processorNames +=  destReq.processorName+"," ;
    }
    return processorNames;
}

看起来这个for循环的工作是跟踪当前元素是否是最后一个元素,从而省略末尾的分隔符。我认为在每次迭代中添加分隔符并在最后"减去"多余的分隔符更容易。

保持list元素唯一的一种方法是使用一个跟踪list元素的数据结构。在as3中,泛型对象的属性类型为String且唯一。

这里有一个小类可以做到这一点:

class StringSet
{
    private var strings:Object = {};
    private var delimiter:String;
    public function StringSet(delimiter:String = ", ")
    {
        this.delimiter = delimiter;
    }
    public function put(element:String):void
    {
        strings[element] = 0;    
    }
    public function toString():String
    {
        var result:String = "";
        for (var string:String in strings)
            result += string + delimiter;
        return result.substring(0, result.length - delimiter.length);
    }
}
现在,代码看起来像这样:
public function processorNameFormat(column:GridColumn):String
{
    var processorNames:StringSet = new StringSet();
    for each(var destReq:ProcessOrderDestinationRequirementDO in processOrderDestinationRequirementDOList)
    {
        processorNames.put(destReq.processorName);
    }
    return processorNames;
}

列出名称并确保它们唯一的所有管理工作现在都在StringSet类中。

我希望代码是自解释的。如果有任何代码不清楚,请在评论中询问。


我假设DO代表数据对象,它通常不包含任何功能。但是这是IMO属于这些对象的功能,因此应该在它们的类中声明。

相关内容

  • 没有找到相关文章

最新更新