Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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: [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?
}
share|improve this question
2  
stop doing brainstorming, and write some code. That would rather help. –  Rohit Jain Oct 25 '13 at 16:04
4  
string.toCharArray(); Now stop thinking and code. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Oct 25 '13 at 16:05
1  
The String documentation would be a good place to start. –  GriffeyDog Oct 25 '13 at 16:07
3  
Why so rude? A beginner can ask questions on StackOverflow, can't he? Sick of all the downvotes I get! –  AmanArora Oct 25 '13 at 16:10
4  
You can ask a question, but you need to show you've made some effort. How can I ... is a bad format for StackOverflow questions. StackOverflow questions should typically be in the following format: This is what I want to accomplish. This is what I've tried. Here is the code that I've actually compiled (or attempted to compile). I'm having this result. I'm expecting this result. What have I done wrong? If you want someone else to write the code to turn an array of Strings into an array of char, then you should hire a programmer. –  nhgrif Oct 25 '13 at 16:12
show 15 more comments

2 Answers

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
    }
}
share|improve this answer
 
Finally, Thank you R.J! –  AmanArora Oct 25 '13 at 16:42
 
How to accept and answer? –  AmanArora Oct 29 '13 at 6:44
add comment

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


    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.