打印出不带方括号的堆栈内容

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

printing out stack without []

问题

以下是您要求的代码部分的翻译结果:

  1. private int numberOfDisks;
  2. private int startPillar;
  3. private int targetPillar;
  4. private int extraPillar;
  5. private Stack<Integer>[] pillars = new Stack[3];
  6. private int steps;
  7. public Hanoi(int n, int start, int target) {
  8. numberOfDisks = n;
  9. startPillar = start - 1;
  10. targetPillar = target;
  11. extraPillar = 6 - start - target;
  12. steps = 0;
  13. pillars[0] = new Stack<Integer>();
  14. pillars[1] = new Stack<Integer>();
  15. pillars[2] = new Stack<Integer>();
  16. for (int i = n; i > 0; i--) {
  17. pillars[startPillar].push(i);
  18. }
  19. printStep();
  20. }
  21. private void printStep() {
  22. for (int i = 1; i < 4; i++) {
  23. System.out.print("t" + steps + " Pillar" + i + ": ");
  24. if (!pillars[i - 1].isEmpty()) {
  25. boolean first = true;
  26. for (Integer disk : pillars[i - 1]) {
  27. if (!first) {
  28. System.out.print(" ");
  29. }
  30. System.out.print(disk);
  31. first = false;
  32. }
  33. }
  34. System.out.println();
  35. }
  36. }

请注意,我对代码进行了必要的调整,以便输出与您所需的格式匹配。

英文:

When I print the stack I want it without [] braces.
What should I change in my code or how can I do it so the output looks like the one that I show below?

here is my output now

  1. t0 Pillar1: [3, 2, 1]
  2. t0 Pillar2: []
  3. t0 Pillar3: []

I want it to be:

  1. t0 Pillar1: 3 2 1
  2. t0 Pillar2:
  3. t0 Pillar3:

here is my code

  1. private int numberOfDisks;
  2. private int startPillar;
  3. private int targetPillar;
  4. private int extraPillar;
  5. private Stack&lt;Integer&gt;[] pillars = new Stack[3];
  6. private int steps;
  7. public Hanoi(int n, int start, int target)
  8. {
  9. numberOfDisks = n;
  10. startPillar = start-1;
  11. targetPillar = target;
  12. extraPillar = 6 - start - target;
  13. steps = 0;
  14. pillars[0] = new Stack&lt;Integer&gt;();
  15. pillars[1] = new Stack&lt;Integer&gt;();
  16. pillars[2] = new Stack&lt;Integer&gt;();
  17. for(int i = n; i &gt; 0; i--)
  18. {
  19. pillars[startPillar].push(i);
  20. }
  21. printStep();
  22. }
  23. private void printStep()
  24. {
  25. for(int i=1;i&lt;4;i++)
  26. {
  27. System.out.println(&quot;t&quot;+steps+&quot; &quot; + &quot;Pillar&quot; + (i) + &quot;: &quot; + pillars[i-1]);
  28. }
  29. }

答案1

得分: 1

  1. IMO 最快的方法是先将其放入一个字符串中
  2. String result = pillars[i-1];
  3. result = result.replace("[", "").replace("]", "");
  4. System.out.println("t" + steps + " " + "Pillar" + (i) + ": " + result);
英文:

IMO the quickest way would be to put it in a String first

  1. String result = pillars[i-1];
  2. result = result.replace (&quot;[&quot;, &quot;&quot;).replace(&quot;]&quot;, &quot;&quot;);
  3. System.out.println(&quot;t&quot;+steps+&quot; &quot; + &quot;Pillar&quot; + (i) + &quot;: &quot; + result);

答案2

得分: 0

使用 Arrays.toString()

  1. System.out.println(Arrays.toString(pillars.toArray()).replace("[","").replace("]",""));
英文:

Use Arrays.toString():

  1. System.out.println(Arrays.toString(pillars.toArray()).replace(&quot;[&quot;,&quot;&quot;).replace(&quot;]&quot;,&quot;&quot;));
  2. </details>

huangapple
  • 本文由 发表于 2020年10月23日 09:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64492694.html
匿名

发表评论

匿名网友

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

确定