How to convert a String array to char array in Java

前端 未结 6 1693
孤城傲影
孤城傲影 2020-12-10 00:33

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:

Input: [i, love, you]

output:

6条回答
  •  感情败类
    2020-12-10 00:43

    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++;
            }
        }
    
    
        }
    

提交回复
热议问题