开发者

Is there a version of the removeElement function in Go for the vector package like Java has in its Vector class?

开发者 https://www.devze.com 2022-12-13 01:37 出处:网络
I am porting over some Java code into Google\'s Go language and I converting all code except I am stuck on just one part after an amazingly smooth port. My Go code looks like this an开发者_JAVA技巧d t

I am porting over some Java code into Google's Go language and I converting all code except I am stuck on just one part after an amazingly smooth port. My Go code looks like this an开发者_JAVA技巧d the section I am talking about is commented out:

func main() {
    var puzzleHistory * vector.Vector;
    puzzleHistory = vector.New(0);
    var puzzle PegPuzzle;
    puzzle.InitPegPuzzle(3,2);
    puzzleHistory.Push(puzzle);

    var copyPuzzle PegPuzzle;
    var currentPuzzle PegPuzzle;

    currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
    isDone := false;
    for !isDone {
        currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
        currentPuzzle.findAllValidMoves();

        for i := 0; i < currentPuzzle.validMoves.Len(); i++ {
            copyPuzzle.NewPegPuzzle(currentPuzzle.holes, currentPuzzle.movesAlreadyDone);
            copyPuzzle.doMove(currentPuzzle.validMoves.At(i).(Move));
            // There is no function in Go's Vector that will remove an element like Java's Vector
            //puzzleHistory.removeElement(currentPuzzle);
            copyPuzzle.findAllValidMoves();
            if copyPuzzle.validMoves.Len() != 0 {
                puzzleHistory.Push(copyPuzzle);
            }
            if copyPuzzle.isSolutionPuzzle() {
                fmt.Printf("Puzzle Solved");
                copyPuzzle.show();
                isDone = true;
            }
        }
    }
}

If there is no version available, which I believe there isn't ... does anyone know how I would go about implementing such a thing on my own?


How about Vector.Delete( i ) ?


Right now Go doesn't support generic equality operators. So you'll have to write something that iterates over the vector and removes the correct one.

0

精彩评论

暂无评论...
验证码 换一张
取 消