本文介绍了如何在Java中将String数组转换为char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们得到一个字符串数组,我们需要一个char[],即所有字符串中所有字符的数组例如:
We are given an array of Strings and what we require is a char[], i.e, array of all characters in all the StringsFor example:
输入:[我,爱,你]
输出:[i, l, o, v, e, y, o, u]
output: [i, l, o, v, e, y, o, u]
首先我制作了一个数组数组.
First I made an array of arrays.
然后我找到了所需的 char[] 数组的长度.
Then I have found the length of the required char[] array.
到目前为止,我已经尝试了以下方法:
I have tried the following so far:
char[][] a1 = new char[str.length][];
for(int i =0;i<str.length;i++){
a1[i]=str[i].toCharArray();
}
int total=0;
for(int i =0;i<str.length;i++){
total = total + a1[i].length;
}
char[] allchar = new char[total];
for(int i=0;i<str.length;i++){
//NOW HERE I WANT TO MERGE ALL THE char[] ARRAYS TOGETHER.
//HOW SHOULD I DO THIS?
}
推荐答案
String[] sArray = {"i", "love", "you"};
String s = "";
for (String n:sArray)
s+= n;
char[] c = s.toCharArray();
这篇关于如何在Java中将String数组转换为char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!