java 如何在java中连接两个char数组?

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

How to concat two char arrays in java?

java

提问by user2477501

How to concatenate two char arrays in java ?

如何在java中连接两个char数组?

char info[]=new char[10];
char data[]=new char[10];
char result[]=new char[40];

I need to concatenate infoand data, and store the concatenation in result:

我需要连接infodata,并将连接存储在result

result=info+data;

result=info+data;

How to do this?

这个怎么做?

回答by MadProgrammer

It depends I guess. The simpler approach would be just to convert the chararrays to a Stringand concaternate the Strings.

这取决于我猜。更简单的方法是将char数组转换为 aString并连接Strings。

A better approach would be to use StringBuilder

更好的方法是使用 StringBuilder

char info[] = new char[10];
char data[] = new char[10];


// Assuming you've filled the char arrays...

StringBuilder sb = new StringBuilder(64);
sb.append(info);
sb.append(data);

char result[] = sb.toString().toCharArray();

回答by Evgeniy Dorofeev

try this

试试这个

char result[] = new char[info.length + data.length];
System.arraycopy(info, 0, result, 0, info.length);
System.arraycopy(data, 0, result, info.length, data.length);

回答by Developer

Just found one-line solution from the old Apache Commons Lang library: ArrayUtils addAll()

刚刚从旧的 Apache Commons Lang 库中找到了一行解决方案:ArrayUtils addAll()