[英]How to convert ArrayList of Strings to char array
The following code converts ArrayList<String>
to char[]
and print output which appears as [back, pack]
. 以下代码将
ArrayList<String>
转换为char[]
并打印输出,显示为[back, pack]
。 Here, the char[]
includes ','
and ' '
. 在这里,
char[]
包含','
和' '
。 Is there a way to do it in another way to get rid of comma and space? 有没有一种方法可以摆脱逗号和空格?
ArrayList<String> list = new ArrayList<String>();
list.add("back");
list.add("pack");
char[] chars = list.toString().toCharArray();
for (char i : chars){
System.out.print(i);
}
You can do it by joining the String
s in your ArrayList<String>
and then getting char[]
from the result: 您可以通过在
ArrayList<String>
加入String
,然后从结果中获取char[]
来实现:
char[] chars = list.stream().collect(Collectors.joining()).toCharArray();
Here .stream.collect(Collectors.joining())
part is Java 8 Stream way to join a sequence of Strings
into one. 这里的
.stream.collect(Collectors.joining())
部分是Java 8 Stream方法,用于将一串Strings
合并为一个。 See: Collectors.joining()
docs. 请参阅:
Collectors.joining()
文档。
If you want any delimiter between the parts in the result, use Collectors.joining(delimiter)
instead. 如果要在结果中各部分之间使用任何定界符,请改用
Collectors.joining(delimiter)
。
There's also an overload which adds prefix and suffix to the result, for example, if you want [
and ]
in it, use Collectors.joining("", "[", "]")
. 还有一个重载,它会在结果中添加前缀和后缀,例如,如果要在结果中添加
[
和]
,请使用Collectors.joining("", "[", "]")
。
Just replace the this line 只需替换此行
char[] chars = list.toString().toCharArray();
with below two lines 以下两行
String str=list.toString().replaceAll(",", "");
char[] chars = str.substring(1, str.length()-1).replaceAll(" ", "").toCharArray();
Your toString
method on list
is what is adding the comma and space, it's a String
representation of your list
. list
上的toString
方法是添加逗号和空格的方法,它是list
的String
表示形式。 As list
is a collection of String
s you don't need to call toString
on it, just iterate through the collection converting each String
into an array of chars using toCharArray
(I assume you will probably want to add all the chars of all the Strings together). 由于
list
是String
的集合,因此您无需在其上调用toString
,只需遍历该集合即可使用toCharArray
将每个String
转换为chars数组(我假设您可能会希望添加所有Strings的所有chars一起)。
String to charArray in Java Code: Java代码中charArray的字符串:
ArrayList<String> list = new ArrayList<String>();
ArrayList<Character> chars = new ArrayList<Character>();
list.add( "back" );
list.add( "pack" );
for ( String string : list )
{
for ( char c : string.toCharArray() )
{
chars.add( c );
}
}
System.out.println( chars );
Just an example how should you resolved large list to array copy. 只是一个示例,您应如何解析大列表到数组副本。 Beware number of characters must be less then Integer.MAX.
注意字符数必须小于Integer.MAX。 This code is just an example how it could be done.
这段代码只是一个示例。 There are plenty of checks that one must implement it to make that code works properly.
为了使代码正常工作,必须进行大量检查。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class WrappedList {
//Or replace with some better counter
int totalCharCount = 0;
final List<String> list;
public WrappedList() {
this(new ArrayList<String>());
}
public WrappedList(final List<String> list) {
this.list = list;
}
public void add(final String toAdd) {
if(toAdd != null) {
totalCharCount += toAdd.length();
this.list.add(toAdd);
}
}
public List<String> getList() {
return Collections.unmodifiableList(list);
}
public char[] toCharArray() {
return this.toCharArray(this.totalCharCount);
}
public char[] toCharArray(final int charCountToCopy) {
final char[] product = new char[charCountToCopy];
int buffered = 0;
for (String string : list) {
char[] charArray = string.toCharArray();
System.arraycopy(charArray, 0, product, buffered, charArray.length);
buffered += charArray.length;
}
return product;
}
//Utility method could be used also as stand-alone class
public char[] toCharArray(final List<String> all) {
int size = all.size();
char[][] cpy = new char[size][];
for (int i = 0; i < all.size(); i++) {
cpy[i] = all.get(i).toCharArray();
}
int total = 0;
for (char[] cs : cpy) {
total += cs.length;
}
return this.toCharArray(total);
}
public static void main(String[] args) {
//Add String iteratively
WrappedList wrappedList = new WrappedList();
wrappedList.add("back");
wrappedList.add("pack");
wrappedList.add("back1");
wrappedList.add("pack1");
final char[] charArray = wrappedList.toCharArray();
System.out.println("Your char array:");
for (char c : charArray) {
System.out.println(c);
}
//Utility method one time for all, should by used in stand-alone Utility class
System.out.println("As util method");
System.out.println(wrappedList.toCharArray(wrappedList.getList()));
}
}
See also: system-arraycopy-than-a-for-loop-for-copying-arrays 另请参见: system-arraycopy而不是循环复制数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.