3

This question was posted here (https://stackoverflow.com/questions/15881110/java-to-c-sharp-conversion) by a team member but was closed due to the community not having enough information.

Here's my attempt to revive such a question being, How would I go about converting this java extract into C#?

Java Extract:

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;
                    }
                }
            );

A sortedList has been thought about but to no avail as I'm unsure how to code it.

I've also tried creating a standard list with a method:

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

and then calling: openList.Sort(CompareFCost); at appropriate locations, however this doesn't work.

What the code is used for? It orders the objects 'PuzzleNode' depending on a score (pathCost) I have set else where in the program. A while loop then operates and pulls the first object from the list. The list needs to be ordered otherwise an object with a higher pathCost could be chosen and the while loop will run for longer. The objective is to pull the lower pathCost from the list.

I ask for a conversion because it works in Java & the rest of the code has pretty much originated from Java.

Any takers? If you need further info I'm happy to discuss it further.

4

2 回答 2

0

我想你可以像这样滥用 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 的答案,但如果重复排序的成本过高,这可能就是您所需要的。

于 2013-04-08T15:00:30.920 回答
0

代码是用来做什么的?它根据我在程序中其他位置设置的分数(pathCost)对对象“PuzzleNode”进行排序。然后一个 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。

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

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

这是我关于 java 与 C# 相比不完整的咆哮,因为它缺少类似的东西LinQ,但我现在不会在 StackOverflow 中做任何咆哮。


我想提到的另一件事是,如果您使用 C# 进行编码,则最好使用C# Naming Conventions,其中Properties是 ProperCased:

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

代替:

public int getPathCost()
public int setPathCost()
于 2013-04-08T15:13:14.700 回答