Java 将 ArrayList 输出到 String 而不出现 [,](括号)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32774059/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:04:00  来源:igfitidea点击:

Output ArrayList to String without [,] (brackets) appearing

javaarraysstringarraylist

提问by Elliander

OK, so I have an ArrayList that I need to return as a String. Right now I am using this approach:

好的,所以我有一个需要作为字符串返回的 ArrayList。现在我正在使用这种方法:

List<Customer> customers = new ArrayList<>();
List<Account> accounts = new ArrayList<>();


public String customerList() 
{
    String list = Arrays.toString(customers.toArray()); 
    return list;
}

public String accountList() 
{ 
    String account = Arrays.toString(accounts.toArray()); 
    return account;
}

This works fine, but the problem is that I get brackets around the text. I am getting stuff like:

这工作正常,但问题是我在文本周围加了括号。我得到类似的东西:

 Customer list:
 --------------
 [Name: Last, First
 , Name: Last, First
 , Name: Last, First
 ]

When I want something like this:

当我想要这样的东西时:

 Customer list:
 --------------
 Name: Last, First
 Name: Last, First
 Name: Last, First

However, unlike similar questions, I don't simply want to output line by line. I need to store the ArrayList in a String without the brackets so I can work with it.

但是,与类似问题不同的是,我不想简单地逐行输出。我需要将 ArrayList 存储在没有括号的字符串中,以便我可以使用它。

EDIT:It should be noted that the one comma in the version I want it to look like was placed there by a method in a different class:

编辑:应该注意的是,我希望它看起来像的版本中的一个逗号是由不同类中的方法放置在那里的:

public final String getName()  
    { 
        return getLast() + ", " + getFirst(); 
    }

EDIT 2:I was able to find a complete solution by adapting the two answers I was given. It was difficult deciding which was the "answer" because I found both useful, but went with the one that I could use more selectively.

编辑 2:通过调整我给出的两个答案,我能够找到一个完整的解决方案。很难决定哪个是“答案”,因为我发现两者都很有用,但选择了我可以更有选择地使用的那个。

public String customerList() 
{
    String list = Arrays.toString(customers.toArray()).replace(", N", "N").replace(", N", "N");
    return list.substring(1,list.length()-1);
}

To remove the brackets I used the modified return. Removing the comma required me to focus on the fact that each line will start with an "N", but the flaw in this approach is that the code would break if I forget to change it here if I change it there. Still, it solves my specific problem, and I can always notate to myself in both places as needed.

为了删除括号,我使用了修改后的返回值。删除逗号需要我关注这样一个事实,即每一行都以“N”开头,但这种方法的缺陷是,如果我在那里更改它,如果我忘记在此处更改它,代码就会中断。尽管如此,它还是解决了我的具体问题,我总是可以根据需要在这两个地方给自己做笔记。

采纳答案by bmarkham

You could try to replace the '[' and ']' with empty space

您可以尝试用空格替换 '[' 和 ']'

String list = Arrays.toString(customers.toArray()).replace("[", "").replace("]", "");

回答by Rodrigo Gomes

A possible solutions is:

一个可能的解决方案是:

String account = Arrays.toString(accounts.toArray()); 
return account.substring(1,account.length()-1);

Or do you override the toStringmethod to return the string as per you wanted.

或者您是否覆盖该toString方法以根据需要返回字符串。

回答by Shivendra Pratap

You can override the toString() method and represent the output in whatever format you

您可以覆盖 toString() 方法并以您想要的任何格式表示输出

回答by Thev

If you want your output to look like: item1, item2, item3

如果您希望您的输出看起来像: item1、item2、item3

Arrays.toString(customers.toArray()).replace('[', ' ').replace(']', ' ').trim()

If you want your output to look like: item1 item2 item3

如果您希望您的输出看起来像: item1 item2 item3

Arrays.toString(customers.toArray()).replace('[', ' ').replace(']', ' ').replace(',', ' ').trim()

回答by Saurabh Mittal

Can directly convert list/set to string and perform action on it

可以直接将列表/集转换为字符串并对其执行操作

customers.toString().replace("[", "").replace("]", "")

回答by Dheeraj vats

You can use the method substring() to remove starting and ending brackets without tampering any entries in the ArrayList. I used something like this to convert a ArrayList<String>to String

您可以使用 substring() 方法删除开始和结束括号,而不会篡改 ArrayList 中的任何条目。我用这样的东西将 a 转换ArrayList<String>为 String

String str = list.toString().substring(1, list.toString().length() - 1);

回答by yejianfengblue

Java 8 version

Java 8 版本

List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(4);
System.out.println(intList.stream().map(i -> i.toString()).collect(Collectors.joining(",")));

Output: 1,2,4

输出:1,2,4

回答by Tayyab Nabeel

Anyone still stuck on this, try using a for-each loop.

任何人仍然坚持这一点,请尝试使用 for-each 循环。

ArrayList<String> list = new ArrayList<>(); // default size of 10

/*add elements to 
the ArrayList*/

for(String element: list)
System.out.println(element);