如何通过格式化的方式在ArrayList中使用toString打印所有元素?

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

How should I go about printing all elements in an ArrayList via a toString in a formatted manner?

问题

我所询问的是,在Java 8中,当我调用我的toString()方法(已重写)时,我试图以以下方式打印出每个相应ArrayList<String>的元素:

- str1
- str2
- str3

但是相反地,当我返回ArrayList<String>时,它以以下方式打印出来:

[str1,str2,str3]

我非常清楚为什么会以那种方式打印,但我不确定如何改变在toString()中的显示方式。
请注意,这个ArrayList<String>的大小因其所属对象的不同而异,并且我不愿意使用外部方法以这种方式打印这些元素,除非没有其他方法可行。

此外,在toString()内部使用for循环以前述方式打印ArrayList<String>的元素,我认为甚至没有意义,而且我认为Java也不会允许这样做。

英文:

What I am asking is that, in Java 8, when I call my toString() method (overridden), I am trying to print out the elements of each respective ArrayList<String> in this manner:

- str1
- str2
- str3

But instead, when I return the ArrayList<String>, it is printed in this manner:

[str1, str2, str3]

It makes perfect sense to me why it is printed that way, but I'm not sure how I would go about changing how it is displayed in the toString().
Note that this ArrayList<String> varies in size depending on the object of which it is apart of, and I would rather not use an external method to print these elements out in such a manner unless there was no other way to do it.

Furthermore, I don't think it even makes sense to have a for loop within a toString() printing out the elements of the ArrayList<String> in the aforementioned manner, and I don't think Java would allow that anyway.

答案1

得分: 1

一种解决方案是编写自定义的 List 实现,用于包装列表,然后重写 toString() 方法。这似乎有些过度设计。我建议使用 Stream API 来打印 List 的条目:

list.forEach(entry -> System.out.println("- " + entry));

<kbd>Ideone 演示</kbd>

随着新的实例方法 String::formatted(目前是预览功能)的推出,我们还可以这样编写:

list.stream()
    .map(&quot;- %s&quot;::formatted)
    .forEach(System.out::println);

(抱歉,此示例没有 <kbd>Ideone 演示</kbd>,因为 Ideone 运行在 Java 12)

英文:

One solution would be to write a custom List-implementation to wrap a list in and then override toString(). This seems pretty overkill. Instead, I suggest printing the entries of the List with the help of the Stream API:

list.forEach(entry -&gt; System.out.println(&quot;- &quot; + entry));

<kbd>Ideone demo</kbd>

With the advent of the new instance method String::formatted (currently a preview feature), we could also write

list.stream()
    .map(&quot;- %s&quot;::formatted)
    .forEach(System.out::println);

(Sorry, no <kbd>Ideone demo</kbd> for this one, Ideone runs on Java 12)

答案2

得分: 0

我会创建另一个方法,类似于displayArrayPretty(ArrayList<String> arr),在这个方法中,您可以循环遍历数组并按您想要的格式打印。例如:

for (String str: arr) {
    System.out.println("- " + str);
}
英文:

I would just create another method like displayArrayPretty(ArrayList<String> arr) where you loop through and print how you want. Ex.

for (String str: arr) {
System.out.println(&quot;- &quot; + str);
}

huangapple
  • 本文由 发表于 2020年9月26日 03:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64070556.html
匿名

发表评论

匿名网友

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

确定