有什么方法可以将字符串数组转换为字符数组?

时间:2016-02-12 05:33:17

标签: java arrays string character

简而言之,我有一系列单词,例如{apple, banana, cat} 而我正在考虑是否可以将其转换为{a, p, p, l, e, b, a, ....} 我不确定它是否可能。

我最初的愚蠢尝试:

for(String s: lelWords)
{
    lelChars.add(s.toCharArray());
}

2 个答案:

答案 0 :(得分:2)

String[] strArray = {"apple", "banana", "cat"};
//combine all words to one string
String combStr = new String();
for(String string: strArray)
    combStr += string;

char[] charArray = combStr.toCharArray();
//checking
System.out.println(charArray);

答案 1 :(得分:-1)

应该很简单

int numChars = 0;

//determine the total number of characters
for (int i = 0; i < strings.length; i++) 
  numChars += strings[i].length();
int idx = 0;

//declare our final array of characters
char[] chars = new char[numChars];

//populate our new array
for (int i = 0; i < strings.length; i++) {
  for (int j = 0; j < strings[i].length; j++) {
    chars[idx] = strings[i].charAt(j);
    idx++;
  }
}