如何在Java中将String数组转换为char数组

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

How to convert a String array to char array in Java

javaarraysstring

提问by AmanArora

We are given an array of Strings and what we require is a char[], i.e, array of all characters in all the Strings For example:

给定一个字符串数组,我们需要的是一个char[],即所有字符串中所有字符的数组,例如:

Input: [i, love, you]

输入:[我,爱,你]

output: [i, l, o, v, e, y, o, u]

输出:[i, l, o, v, e, y, o, u]

First I made an array of arrays.

首先,我制作了一个数组数组。

Then I have found the length of the required char[] array.

然后我找到了所需的 char[] 数组的长度。

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?
}

回答by SudoRahul

You can do that like this

你可以这样做

char[] allchar = new char[total]; // Your code till here is proper

// Copying the contents of the 2d array to a new 1d array
int counter = 0; // Counter as the index of your allChar array
for (int i = 0; i < a1.length; i++) { 
    for (int j = 0; j < a1[i].length; j++) { // nested for loop - typical 2d array format
        allchar[counter++] = a1[i][j]; // copying it to the new array
    }
}

回答by Eduardo Dennis

You can do something like the following method..

您可以执行类似以下方法的操作..

    public void converter(String[] stringArray) {


    char[] charsInArray = new char[500];    //set size of char array    
    int counterChars = 0;

    for (int i = 0; i < stringArray.length; i++) { 

        int counterLetters = 0; //declare counter for for amount of letters in each string in the array 

        for (int j = 0; j < stringArray[i].length(); j++) { 


            // below pretty self explanatory extracting individual strings and a
            charsInArray[counterChars] = stringArray[i].charAt(counterLetters);

            counterLetters++;
            counterChars++;
        }
    }


    }

回答by Faraz

String [] s = new String[]{"i", "love", "you"};

int length = 0;
for (int i = 0; i < s.length; i++) {        
    for (int j = 0; j < s[i].length(); j++) {
        length++;
    }
}

char [] c = new char[length];
int k = 0;
for (int i = 0; i < s.length; i++) {        
    for (int j = 0; j < s[i].length(); j++) {
        c[k] = s[i].charAt(j);
        k++;
    }
}
for (int j = 0; j < c.length; j++) {
    System.out.print("char is: " + c[j]);
}

回答by Dan Rothman

String[] sArray = {"i", "love", "you"};
    String s = "";
    for (String n:sArray)
        s+= n;
    char[] c = s.toCharArray();

回答by Gokul Krishna gK

public static void main(String[] args)
{
    String sentence="Gokul Krishna is class leader";
    String[] array=sentence.split("\s");
    int counter;
    //String word = new String();
    for(String word:array)
    {
        counter=0;
        char[] wordArray=word.toCharArray();
        for(int i=0;i<wordArray.length;i++)
        {
            counter++;
        }
        System.out.println(word+ " :" +counter);
    }
}