Java:打印没有方括号的LinkedList? [英] Java: Printing LinkedList without square brackets?

查看:670
本文介绍了Java:打印没有方括号的LinkedList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个相当简单的问题。当你打印一个LinkedList时,像这样:

This is a fairly simple question. When you print out a LinkedList, like so:

System.out.println(list);

它打印出来,用方括号括起来,如下:

It prints it out, surrounding the list in square brackets like this:

[thing 1, thing 2, thing 3]


b $ b

有没有方法可以打印出来没有方括号?

Is there a way I can print it out without the square brackets?

推荐答案

并打印它(每个后面带逗号,但最后一个元素)。但是,有一些工具可以帮助:

Yes - iterate the list and print it (with comma after each, but the last element). However, there are utils to help:

Guava

String result = Joiner.on(", ").join(list);

commons- lang

String result = StringUtils.join(list, ", ");

并且注意:不要依赖 .toString / code>任何对象的方法。它不是用于向用户显示对象,或用作预定义的格式 - 它主要用于调试目的。

And one note: don't rely on the .toString() method of any object. It is not meant for displaying the object to users, or to be used as a predefined format - it is meant mainly for debugging purposes.

这篇关于Java:打印没有方括号的LinkedList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆