在Go语言的vector包中是否有类似Java中Vector类的removeElement函数的版本?

huangapple go评论121阅读模式
英文:

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

问题

我正在将一些Java代码移植到Google的Go语言中,除了一个非常顺利的移植之外,我正在转换所有的代码,但我卡在了一个部分。我的Go代码如下,我所说的部分被注释掉了:

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));
			// Go语言的Vector没有像Java的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;
			}
		}
	}
}

如果没有可用的版本,我相信没有...有人知道我该如何自己实现这样的功能吗?

英文:

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 and 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 &lt; currentPuzzle.validMoves.Len(); i++ {
			copyPuzzle.NewPegPuzzle(currentPuzzle.holes, currentPuzzle.movesAlreadyDone);
			copyPuzzle.doMove(currentPuzzle.validMoves.At(i).(Move));
			// There is no function in Go&#39;s Vector that will remove an element like Java&#39;s Vector
			//puzzleHistory.removeElement(currentPuzzle);
			copyPuzzle.findAllValidMoves();
			if copyPuzzle.validMoves.Len() != 0 {
				puzzleHistory.Push(copyPuzzle);
			}
			if copyPuzzle.isSolutionPuzzle() {
				fmt.Printf(&quot;Puzzle Solved&quot;);
				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?

答案1

得分: 0

如何使用Vector.Delete(i)?

英文:

How about Vector.Delete( i ) ?

答案2

得分: 0

目前Go语言不支持泛型相等运算符。因此,您需要编写一个迭代向量并删除正确元素的代码。

英文:

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.

huangapple
  • 本文由 发表于 2009年11月26日 15:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/1802102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定