简体   繁体   中英

How to convert a String array to char array in Java

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

Input: [i, love, you]

output: [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.

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();

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

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


    }
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]);
}
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);
    }
}

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.

public char[] convert(String[] arr) {
    StringBuilder sb = new StringBuilder();
    for (String s : arr) {
        sb.append(s);
    }
    return sb.toString().toCharArray();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM