【问题标题】:JAVA to C# conversion - PriorityQueueJAVA 到 C# 的转换 - PriorityQueue
【发布时间】:2013-03-30 17:02:40
【问题描述】:

此问题由一名团队成员在此处 (https://stackoverflow.com/questions/15881110/java-to-c-sharp-conversion) 发布,但由于社区没有足够的信息而被关闭。

这是我试图重新提出这样一个问题的尝试,我将如何将此 java 提取转换为 C#?

Java 提取:

PriorityQueue<PuzzleNode> openList = new PriorityQueue<PuzzleNode>
           (1,
            new Comparator<PuzzleNode>(){
                public int compare(PuzzleNode a, PuzzleNode b){
                    if (a.getPathCost() > b.getPathCost())
                        return 1;
                    else if (a.getPathCost() < b.getPathCost())
                        return -1;
                    else
                        return 0;
                    }
                }
            );

已经考虑过 sortedList 但无济于事,因为我不确定如何对其进行编码。

我也尝试过使用方法创建标准列表:

List<PuzzleNode> openList = new List<PuzzleNode>();

//Method to sort the list
public int CompareFCost(PuzzleNode a, PuzzleNode b)
        {
            if (a.getPathCost() > b.getPathCost())
            {
                return 1;
            }
            else if (a.getPathCost() > b.getPathCost())
            {
                return -1;
            }
            else
                return 0;
        }//end CompareFCost

然后调用:openList.Sort(CompareFCost);在适当的位置,但这不起作用。

代码的用途是什么? 它根据我在程序中其他位置设置的分数(pathCost)对对象“PuzzleNode”进行排序。然后一个 while 循环运行并从列表中拉出第一个对象。需要对列表进行排序,否则可以选择具有更高 pathCost 的对象,并且 while 循环将运行更长时间。目标是从列表中拉出较低的 pathCost。

我要求进行转换,因为它可以在 Java 中运行,并且其余代码几乎都源自 Java。

有接受者吗?如果您需要更多信息,我很乐意进一步讨论。

【问题讨论】:

  • 也许这个答案会有所帮助:stackoverflow.com/questions/102398/priority-queue-in-net 我认为唯一的问题是 PriorityQueue 的替代方案,它与简单的有序列表完全不同。
  • 这样做的最终目的是什么?你知道你可以使用LinQ 来做各种IEnumerable&lt;T&gt; 相关的操作吗?另外,java的PriorityQueue&lt;T&gt;和普通的List&lt;T&gt;有什么区别?
  • @HighCore - 优先队列允许有效访问和删除最小元素。
  • @Lee 和“最小元素”你的意思是......?
  • 假设您使用 List 并且只是根据 PathCost 进行排序,您可以使用 linq:var orderedList = openList.OrderBy(node =&gt; node.getPathCost())(如果需要采用其他方式,则可以使用.OrderByDescending)。或者,如果您想更好地控制比较,请查看“按比较器排序”示例:code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

标签: c# java list


【解决方案1】:

我想你可以像这样盗用 SortedList:

var openList=new SortedList<PuzzleNode,PuzzleNode>(
    //assumes .Net4.5 for Comparer.Create
    Comparer<PuzzleNode>.Create((a,b)=>{
        if (a.getPathCost() > b.getPathCost())
                    return 1;
                else if (a.getPathCost() < b.getPathCost())
                    return -1;
                else
                    return 0;

    }));
openList.Add(new PuzzleNode());
foreach(var x in openList.Keys)
{
    //ordered enumeration
}
var firstItem = openList.Dequeue();

通过创建一些扩展方法使事情更像队列

static class SortedListExtensions
{
    public static void Add<T>(this SortedList<T,T> list,T item)
    {
        list.Add(item,item);
    }
    public static T Dequeue<T>(this SortedList<T,T> list)
    {
        var item=list.Keys.First();
        list.Remove(item);
        return item;
    }
    //and so on...
}

TBH,我可能会在对您原始问题的评论中寻求@valverij 的答案,但如果重复排序的成本过高,这可能就是您所需要的。

【讨论】:

    【解决方案2】:

    代码的用途是什么?它对“PuzzleNode”对象进行排序 根据我在程序中的其他位置设置的分数(pathCost)。 然后一个 while 循环运行并从列表中拉出第一个对象。 该列表需要排序,否则具有更高的对象 可以选择 pathCost 并且 while 循环将运行更长时间。这 目标是从列表中拉出较低的 pathCost。

    • 1:有LinQ。您通常不会在 C# 中做任何这些事情,因为 LinQ 会为您完成。

      • 它根据分数 (pathCost) 对对象“PuzzleNode”进行排序

        这是通过 LinQ 的 Enumerable.OrderBy() 扩展实现的:

        //Assuming PathCost is a property of a primitive type (int, double, string, etc)
        var orderedlist = list.OrderBy(x => x.PathCost);
        
      • 目标是从列表中拉出较低的 pathCost。

        这是使用 LinQ 的 Enumerable.Min()Enumerable.Max() 扩展实现的。

        //Same assumption as above.
        var puzzlewithlowestpath = list.Min(x => x.PathCost);
        

    我在这里抱怨 java 与 C# 相比是不完整的,因为它缺少像 LinQ 这样的东西,但我现在不会再在 StackOverflow 上抱怨了。


    我想提的另一件事是,如果您使用 C# 编码,最好使用C# Naming Conventions,其中Properties 是正确大小写的:

    public int PathCost {get;set;}
    //or double or whatever
    

    代替:

    public int getPathCost()
    public int setPathCost()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2020-02-26
      • 1970-01-01
      相关资源
      最近更新 更多