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

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

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代码如下,我所说的部分被注释掉了:

  1. func main() {
  2. var puzzleHistory *vector.Vector;
  3. puzzleHistory = vector.New(0);
  4. var puzzle PegPuzzle;
  5. puzzle.InitPegPuzzle(3,2);
  6. puzzleHistory.Push(puzzle);
  7. var copyPuzzle PegPuzzle;
  8. var currentPuzzle PegPuzzle;
  9. currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
  10. isDone := false;
  11. for !isDone {
  12. currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
  13. currentPuzzle.findAllValidMoves();
  14. for i := 0; i < currentPuzzle.validMoves.Len(); i++ {
  15. copyPuzzle.NewPegPuzzle(currentPuzzle.holes, currentPuzzle.movesAlreadyDone);
  16. copyPuzzle.doMove(currentPuzzle.validMoves.At(i).(Move));
  17. // Go语言的Vector没有像Java的Vector那样可以删除元素的函数
  18. //puzzleHistory.removeElement(currentPuzzle);
  19. copyPuzzle.findAllValidMoves();
  20. if copyPuzzle.validMoves.Len() != 0 {
  21. puzzleHistory.Push(copyPuzzle);
  22. }
  23. if copyPuzzle.isSolutionPuzzle() {
  24. fmt.Printf("Puzzle Solved");
  25. copyPuzzle.show();
  26. isDone = true;
  27. }
  28. }
  29. }
  30. }

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

英文:

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:

  1. func main() {
  2. var puzzleHistory * vector.Vector;
  3. puzzleHistory = vector.New(0);
  4. var puzzle PegPuzzle;
  5. puzzle.InitPegPuzzle(3,2);
  6. puzzleHistory.Push(puzzle);
  7. var copyPuzzle PegPuzzle;
  8. var currentPuzzle PegPuzzle;
  9. currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
  10. isDone := false;
  11. for !isDone {
  12. currentPuzzle = puzzleHistory.At(0).(PegPuzzle);
  13. currentPuzzle.findAllValidMoves();
  14. for i := 0; i &lt; currentPuzzle.validMoves.Len(); i++ {
  15. copyPuzzle.NewPegPuzzle(currentPuzzle.holes, currentPuzzle.movesAlreadyDone);
  16. copyPuzzle.doMove(currentPuzzle.validMoves.At(i).(Move));
  17. // There is no function in Go&#39;s Vector that will remove an element like Java&#39;s Vector
  18. //puzzleHistory.removeElement(currentPuzzle);
  19. copyPuzzle.findAllValidMoves();
  20. if copyPuzzle.validMoves.Len() != 0 {
  21. puzzleHistory.Push(copyPuzzle);
  22. }
  23. if copyPuzzle.isSolutionPuzzle() {
  24. fmt.Printf(&quot;Puzzle Solved&quot;);
  25. copyPuzzle.show();
  26. isDone = true;
  27. }
  28. }
  29. }
  30. }

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:

确定