本文介绍了很好地打印Java收藏(toString不返回漂亮的输出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望像Eclipse调试器一样打印一个 Stack< Integer> 对象(即 [1,2,3 ...] ] ),但是用 out =output:+ stack 打印它不会返回这个不错的结果。

I wish to print a Stack<Integer> object as nicely as the Eclipse debugger does (i.e. [1,2,3...]) but printing it with out = "output:" + stack doesn't return this nice result.

只是为了澄清,我在谈论Java的内置集合,所以我不能覆盖它的 toString()

Just to clarify, I'm talking about Java's built-in collection so I can't override its toString().

如何获得一个漂亮的可打印版本的堆栈?

How can I get a nice printable version of the stack?

推荐答案

您可以转换数组,然后使用 Arrays.toString(Object [])打印出来:

You could convert it to an array and then print that out with Arrays.toString(Object[]):

System.out.println(Arrays.toString(stack.toArray()));

这篇关于很好地打印Java收藏(toString不返回漂亮的输出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:47