将C#方法转换为Java(Android Studio)

时间:2017-05-13 08:12:05

标签: java c# android-studio

这是我试图转换的C#代码

ArrayList.BinarySearch(Object, IComparer)

它的作用是将节点类对象与比较类对象进行比较,并返回-1,0或正整数。 我搜索了很多,并尝试在java中转换它但不能这样做。我需要一种方法,指令,库或任何可以帮助我实现它的东西。

类Class1     {

    [STAThread]
    static void Main(string[] args)
    {

        ArrayList SolutionPathList = new ArrayList();

        //Create a node containing the goal state node_goal
        Node node_goal = new Node(null,null,1,15,15);

        //Create a node containing the start state node_start
        Node node_start = new Node(null,node_goal,1,0,0);


        //Create OPEN and CLOSED list
        SortedCostNodeList OPEN = new SortedCostNodeList ();
        SortedCostNodeList CLOSED = new SortedCostNodeList ();


        //Put node_start on the OPEN list
        OPEN.push (node_start);
        }

SortedNode类是:

 public class SortedCostNodeList
{
    ArrayList _list;
    NodeComparer _nodeComparer;
 public SortedCostNodeList()
    {
        _list = new ArrayList ();
        _nodeComparer = new NodeComparer ();
    }
  public int push (Node n)
    {

        int k = _list.BinarySearch (n,_nodeComparer);

        if (k==-1) // no element
            _list.Insert (0,n);
        else if (k<0) // find location by complement
        {
            k=~k;
            _list.Insert (k,n);
        }
        else if (k>=0)
            _list.Insert (k,n);

        return k;
    }

和nodecomparer类是:

  public class NodeComparer:IComparer
{
    public NodeComparer()
    {

    }

    public int Compare(object x, object y)
    {
        return ((Node)x).totalCost -  ((Node) y).totalCost ;
    }
}

我无法实现这段代码        // int k = _list.BinarySearch(n,_nodeComparer);

任何帮助?

1 个答案:

答案 0 :(得分:0)

您可以使用Arrays.binarySearch()对Object数组执行二进制搜索。

我创建了一个示例程序。希望它有所帮助。

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

@Getter @Setter
@AllArgsConstructor
class Node {
    private long totalCost;
}

public class StackOverFlow {
    public static void main(String... args) {
        final List<Node> list = new ArrayList<>();
        list.add(new Node(10));
        list.add(new Node(20));
        list.add(new Node(30));
        list.add(new Node(40));
        list.add(new Node(50));
        list.add(new Node(60));
        list.add(new Node(70));

        final Node searchKey = new Node(60);
        int index = Arrays.binarySearch(list.toArray(new Node[list.size()]), searchKey, new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return (int) (o1.getTotalCost() - o2.getTotalCost());
            }
        });

        System.out.print("Found at " + index);
    }
}

<强>更新 试试这个

class NodeComparator implements Comparator<Node> {
    @Override
    public int compare(Node o1, Node o2) {
        return (int) (o1.getTotalCost() - o2.getTotalCost());
    }
}

class SortedCostNodeList {
    ArrayList<Node> list;
    NodeComparator nodeCompartor;

    public SortedCostNodeList() {
        this.list = new ArrayList<>();
        this.nodeCompartor = new NodeComparator();
    }

    public int push(Node n) {

        int k = Arrays.binarySearch(this.list.toArray(new Node[list.size()]), n, this.nodeCompartor);

        if (k == -1) // no element
            this.list.add(0, n);
        else if (k < 0) // find location by complement
        {
            k = ~k;
            this.list.add(k, n);
        } else if (k >= 0)
            this.list.add(k, n);

        return k;
    }
    // ...
}