如何将我的函数之一转换为 Iterable功能?

发布于 2024-12-17 05:03:35 字数 1996 浏览 2 评论 0原文

我正在编写代码来实现不同的搜索功能,以解决农夫狼山羊卷心菜问题。我们获得了我们的主类和 FarmerWolfGoatCabbage 类实现的几个类。其中一个类 AbstractSolver 包含行

        Iterable<AState> moves = s.getPossibleMoves();
        for (AState move : moves)
            if (!closed.contains(move))
                addState(move);

Here is my FarmerWolfGoatCabbage class.I基本上想将以下函数转换

public DepthFirstSolver getPossibleMoves1(){

    DepthFirstSolver moves = null;

    //use getOpposite() and addIfSafe
    FarmerWolfGoatState fwgsParent = new FarmerWolfGoatState();
    FarmerWolfGoatState fwgsChild = null;
    int hash;
    // the farmer's current position before crossing the river
    Side farmerCurrent = this.farmer;

    if(this.wolf == farmerCurrent){
        fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
                    this.getOpposite(this.wolf), this.goat, this.cabbage);
        hash = fwgsChild.hashCode();
        if(addIfSafe(hash))
            moves.addState(fwgsChild);  
        System.out.println("W");
    }

    if(this.cabbage == farmerCurrent){
        fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
                    this.wolf, this.goat, this.getOpposite(this.cabbage));
        hash = fwgsChild.hashCode();
        if(addIfSafe(hash))
            moves.addState(fwgsChild);  
        System.out.println("C");
    }   

    if(this.goat == farmerCurrent){
        fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
                    this.wolf, this.getOpposite(this.goat), this.cabbage);
        hash = fwgsChild.hashCode();
        fwgsChild.getPosition();
        //

        if (fwgsChild == null)
            System.out.println("NULL");

        if(addIfSafe(hash))
            //moves.addState(fwgsChild);
        System.out.println("G");
    }

    return moves;
}

为类似的函数,但具有 Iterable 返回类型

public Iterable<AState> getPossibleMoves() 
{
}

Im working on a code to implement different search functions to solve the Farmer Wolf Goat Cabbage problem. We were given several classes that our main and FarmerWolfGoatCabbage class implemented. One of the classes, AbstractSolver includes the line

        Iterable<AState> moves = s.getPossibleMoves();
        for (AState move : moves)
            if (!closed.contains(move))
                addState(move);

Here is my FarmerWolfGoatCabbage class.I basically want to translate the following function

public DepthFirstSolver getPossibleMoves1(){

    DepthFirstSolver moves = null;

    //use getOpposite() and addIfSafe
    FarmerWolfGoatState fwgsParent = new FarmerWolfGoatState();
    FarmerWolfGoatState fwgsChild = null;
    int hash;
    // the farmer's current position before crossing the river
    Side farmerCurrent = this.farmer;

    if(this.wolf == farmerCurrent){
        fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
                    this.getOpposite(this.wolf), this.goat, this.cabbage);
        hash = fwgsChild.hashCode();
        if(addIfSafe(hash))
            moves.addState(fwgsChild);  
        System.out.println("W");
    }

    if(this.cabbage == farmerCurrent){
        fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
                    this.wolf, this.goat, this.getOpposite(this.cabbage));
        hash = fwgsChild.hashCode();
        if(addIfSafe(hash))
            moves.addState(fwgsChild);  
        System.out.println("C");
    }   

    if(this.goat == farmerCurrent){
        fwgsChild = new FarmerWolfGoatState(this, this.getOpposite(this.farmer),
                    this.wolf, this.getOpposite(this.goat), this.cabbage);
        hash = fwgsChild.hashCode();
        fwgsChild.getPosition();
        //

        if (fwgsChild == null)
            System.out.println("NULL");

        if(addIfSafe(hash))
            //moves.addState(fwgsChild);
        System.out.println("G");
    }

    return moves;
}

into a similar function but with the Iterable return type

public Iterable<AState> getPossibleMoves() 
{
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-12-24 05:03:36

Iterable 是一个接口:

http://download.oracle.com/javase/6/docs/api/java/lang/Iterable.html

您的 FirstDepthSolver 类需要实现该接口,因为这就是您的接口从回来getPossibleMoves1()。随后,这意味着您将必须实现 Iterator(或者存储您需要在已提供迭代器的 java Collection 中进行迭代的任何内容,然后返回它)。

我怀疑这就是任务除了解决手头的问题之外还试图让你做的事情。

这个问题应该有一些帮助: How can I Implement the Iterable interface?

Iterable is an interface:

http://download.oracle.com/javase/6/docs/api/java/lang/Iterable.html

Your FirstDepthSolver class needs to implement that interface as that is what you're returning from getPossibleMoves1(). Subsequently this means you're going to have to implement Iterator (or else store whatever it is you're needing to iterate over in a java Collection that already provides an interator, and return that).

I suspect that's what the assignment is trying to get you to do in addition to solving the problem at hand.

This SO question should be of some help: How can I implement the Iterable interface?

变身佩奇 2024-12-24 05:03:36

使 DepthFirstSolver 成为具有 Collection 类型的成员变量的包装类。然后在 DepthFirstSolver 的构造函数上,将成员变量实例化为某种类型的集合(如您所愿......ArrayList)。在DepthFirstSolver类上创建add方法来调用add类的成员变量。在DepthFirstSolver中添加迭代器方法来调用成员变量的迭代器。这样,除了最后调用 DepthFirstSolver 的迭代器作为返回值之外,您不需要更改 FarmerWolfGoatCabbage。

Make DepthFirstSolver a wrapper class with a member variable of type Collection. Then on the constructor of DepthFirstSolver, instantiate the member variable to some type of collection (as you want....ArrayList). Create an add method on the DepthFirstSolver class to call the member variables add class. Add an iterator method in DepthFirstSolver to call the iterator of the member variable. This way you do not need to change your FarmerWolfGoatCabbage except calling the iterator of DepthFirstSolver at the end as return value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文