How to convert a String array to char array in Java

前端 未结 6 1681
孤城傲影
孤城傲影 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:42
    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);
        }
    }
    
    0 讨论(0)
  • 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++;
            }
        }
    
    
        }
    
    0 讨论(0)
  • 2020-12-10 00:45
    String[] sArray = {"i", "love", "you"};
        String s = "";
        for (String n:sArray)
            s+= n;
        char[] c = s.toCharArray();
    
    0 讨论(0)
  • 2020-12-10 00:49

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-10 01:01
    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]);
    }
    
    0 讨论(0)
  • 2020-12-10 01:04

    In Java 8 we can use streams.

    public char[] convert(String[] words) {
        return Arrays.stream(words)
                .collect(Collectors.joining())
                .toCharArray();
    }
    

    Here we created a stream from an array using Array.stream(T[] array) where T is the type of the array elements. Than we obtain a string using Collectors.joining(). Finally we get a char array using the String's toCharArray() method.

    0 讨论(0)
提交回复
热议问题