Java 如何从 ArrayList#toString() 中删除括号 []?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31718178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove the brackets [ ] from ArrayList#toString()?
提问by Goldengirl
I have created an Array List in Java that looks something like this:
我在 Java 中创建了一个数组列表,如下所示:
public static ArrayList<Integer> error = new ArrayList<>();
for (int x= 1; x<10; x++)
{
errors.add(x);
}
When I print errors I get it errors as
当我打印错误时,我得到它的错误
[1,2,3,4,5,6,7,8,9]
[1,2,3,4,5,6,7,8,9]
Now I want to remove the brackets([ ])from this array list. I thought I could use the method errors.remove("["), but then I discovered that it is just boolean and displays a true or false. Could somebody suggest how can I achieve this?
现在我想从这个数组列表中删除括号([])。我以为我可以使用方法errors.remove("["),但后来我发现它只是布尔值并显示真或假。有人可以建议我如何实现这一目标吗?
Thank you in advance for your help.
预先感谢您的帮助。
采纳答案by Adriaan Koster
You are probably calling System.out.printlnto print the list. The JavaDoc says:
您可能正在调用System.out.println来打印列表。JavaDoc 说:
This method calls at first String.valueOf(x) to get the printed object's string value
The brackets are added by the toString
implementation of ArrayList. To remove them, you have to first get the String:
括号是通过toString
ArrayList的实现添加的。要删除它们,您必须首先获取字符串:
String errorDisplay = errors.toString();
and then strip the brackets, something like this:
然后去掉括号,如下所示:
errorDisplay = errorDisplay.substring(1, errorDisplay.length() - 1);
It is not good practice to depend on a toString()
implementation. toString()
is intended only to generate a human readable representation for logging or debugging purposes. So it is better to build the String yourself whilst iterating:
依赖于toString()
实现不是好的做法。toString()
仅用于为日志记录或调试目的生成人类可读的表示。所以最好在迭代时自己构建字符串:
List<Integer> errors = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int x = 1; x<10; x++) {
errors.add(x);
sb.append(x).append(",");
}
sb.setLength(sb.length() - 1);
String errorDisplay = sb.toString();
Note that this is not an array, just a String displaying the contents of the list. To create an array from a list you can use list.toArray():
请注意,这不是一个数组,只是一个显示列表内容的字符串。要从列表创建数组,您可以使用list.toArray():
// create a new array with the same size as the list
Integer[] errorsArray = new Integer[errors.size()];
// fill the array
errors.toArray(errorsArray );
回答by nhb.sajol
This is an ArrayList
of Integer
. This ArrayList
can not contain a character like '['
. But you can remove an Integer
from it like this -
这是一个ArrayList
的Integer
。这ArrayList
不能包含像'['
. 但是你可以Integer
像这样从中删除一个-
error.remove(3);
回答by kai
If you print your error list, it will internally call the toString()
method of your list and this method add the brackets. There are a few possibilities. You can get the String via toString()
method an remove the brackets from the String. Or you write your own method to print the List.
如果您打印错误列表,它将在内部调用toString()
列表的方法,并且此方法会添加括号。有几种可能性。您可以通过toString()
方法获取字符串并从字符串中删除括号。或者您编写自己的方法来打印列表。
public static <T> void printList(List<T> list)
{
StringBuilder output = new StringBuilder();
for(T element : list)
output.append(element + ", ");
System.out.println(output);
}
回答by dotvav
Short answer: System.out.println(errors.toString().substring(1, errors.toString().length() - 1))
简短的回答: System.out.println(errors.toString().substring(1, errors.toString().length() - 1))
Explanation: when you call System.out.println(obj)
with an Object
as a parameter, the printed text will be the result of obj.toString()
. ArrayList.toString()
is implemented in a way that makes it represent its content between brackets []
in a comma separated concatenation of each of the contained items (their .toString()
representation as well).
说明:当您System.out.println(obj)
以 anObject
作为参数调用时,打印的文本将是 obj.toString()
. ArrayList.toString()
以一种方式实现,使其在括号之间[]
以逗号分隔的每个包含项目(它们的.toString()
表示)的串联方式表示其内容。
It is not a good practice to rely on another class's toString()
implementation. You should use your own way to format your result.
依赖另一个类的toString()
实现不是一个好习惯。您应该使用自己的方式来格式化结果。
回答by SatyaTNV
String text = errors.toString().replace("[", "").replace("]", "");//remove brackets([) convert it to string
回答by Igor Iris
brackets is not a part of your array list, since as you've mentioned it's Integer typed
括号不是您的数组列表的一部分,因为正如您所提到的,它是整数类型的
ArrayList<Integer>
ArrayList<Integer>
when you print errors using
当您使用打印错误时
System.out.println(errors);
it's just formatting your data, just iterate over the array and print each value separately
它只是格式化您的数据,只需遍历数组并分别打印每个值
回答by Jan
The brackets are not actually within the list it's just a representation of the list. If any object goes into a String output the objects toString() method gets called. In case of ArrayList this method delivers the content of the list wrapped by this brackets.
括号实际上不在列表中,它只是列表的表示。如果任何对象进入 String 输出,则调用对象 toString() 方法。在 ArrayList 的情况下,此方法提供由此方括号包裹的列表内容。
If you want to print your ArrayList without brackets just iterate over it and print it.
如果您想打印不带括号的 ArrayList,只需对其进行迭代并打印即可。
回答by faflo10
The brackets you see are just an automatic way to display a List in JAVA (when using System.out.println(list);
for example.
您看到的括号只是在 JAVA 中显示 List 的一种自动方式(System.out.println(list);
例如使用时。
If you do not want them to show when showing it, you can create a custom method :
如果您不希望它们在显示时显示,您可以创建一个自定义方法:
public void showList(List<Integer> listInt)
{
for(int el : listInt)
{
System.out.print(el + ", ");
}
}
Then adjust this code to show this according to your liking !
然后根据您的喜好调整此代码以显示此内容!
回答by thomas.g
There are not brackets inside your list. This is just the way Java prints a list by default.
您的列表中没有括号。这就是 Java 默认打印列表的方式。
If you want to print the content of your list, you can something like this
如果你想打印列表的内容,你可以这样
for (Integer error : errors) {
System.out.format("%d ", error);
}
回答by user1740241
System.out.println(error.toString().substring(1, error.toString().length()-1));
This worked for me
System.out.println(error.toString().substring(1, error.toString().length()-1));
这对我有用