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.

I have following piece of code and in this code I want to convert the String array into dynamically generated char array based on the length of String array. Is my approach/technique is feasible or i have to choose some other datatype...actually i have to perform logic on each and every character of word thats why i am breaking the String array to char[] array....

private String[] function(String[] words) {
    String[] names = words;

    char[] CArray= null;

    for (int i = 0; i < names.length ; i++){
        CArray = names[i].toCharArray();
    }

    for (int i = 0; i < CArray.length ; i++){
        System.out.print(CArray[i]);
    }

    return names;           
}

Guys plz try to under stand that I want the names of char[] array to be generated in sequence quto based on the length of String array means there should be 4 char[] arrays if there are 4 words having name in a sequence such as S0,S1,S2,S3 or anything else..!!!!!

Okay guys plz this is what I want to achieve
char[] S1 or anyname = words[0].toCharArray();
char[] S2 or anyname = words[1].toCharArray();
so on...depending upon the no of String arrays passed,,,such that I am left with char[] arrays S1,S2,S3 to carry my further operationss.....plz plz now figure out it ..!! Hope i made clear understanding of the ques..??

share|improve this question
1  
Why not use the charAt method which String class provides to access its each character? –  Aman Agnihotri Feb 22 '14 at 5:58
    
Could you plz show @AwfullyAwesome how can i do that..!! Tried several things bro now i have fed_up..!! –  Gurjit Feb 22 '14 at 6:00
    
Alright, what is it that you want this function to do? I'll write its code, once you tell me, to show how its done. –  Aman Agnihotri Feb 22 '14 at 6:03
    
I actually want the output to be like char[] name = words[0] and char[] name1 = words[1],,,means name of char array to be generate auto based on the length of String array –  Gurjit Feb 22 '14 at 6:08
    
Check out my answer then. –  Aman Agnihotri Feb 22 '14 at 6:17

4 Answers 4

Here's a program then to demonstrate what you asked:

public class CharArray {

public CharArray() {
    String[] words = { "First", "Second" };

    for (String word : words) {
        for (char character : word.toCharArray()) {
            System.out.print(character + " ");
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    new CharArray();
}
}

The words present in the word array will come one by one to the character variable in the constructor. Do whatever you want to do with them there.

Hope it helps.

share|improve this answer
    
Is it possible to give 2 counter initialization in loop..?? –  Gurjit Feb 22 '14 at 6:21
    
Yes it is possible to give multiple initialisation in the loop, which need to be separated by comma. You can even write multiple update expressions separated by comma, and multiple conditions by using &&, || operators. –  Aman Agnihotri Feb 22 '14 at 6:23
    
Here's an example: for(int i = 0, j = 100; i < 10 && j > 10; i++, j-=2) { //Do some crazy stuff with i and j } –  Aman Agnihotri Feb 22 '14 at 6:25

I am afraid this is not even close.. did you execute the code and see its results?

  1. Not to be nit picking, but this is sort of important: variable names in Java begin with lower case, so cArray instead of CArray. Saves readers a lot of confusion (is it a variable? Is it a class? etc).

  2. What's the purpose of this line? Why not just use words?

    String[] names = words;

  3. The code below results in CArray to be an array of characters comprising the LAST word. All other words but the last one are discarded.

for (int i = 0; i < names.length ; i++){ CArray = names[i].toCharArray();

Perhaps you meant to nest the second loop into the body of the first loop?

  1. And then you are returning names, which is precisely the same as words, no char arrays there.

  2. Perhaps you want function to return an array of arrays of chars?

  3. I am not sure you really want to convert into an array of arrays of chars, as opposed to, say, using StringBuffer or regular expressions. Most text manipulation in Java is done this way.

I recommend studying more before plunging into coding..

share|improve this answer
    
Yes ofcourse,,I know and that words have quite a role further in the function...!!I actually want the output to be like char[] name = words[0] and char[] name1 = words[1],,,means name of char array to be generate auto based on the length of String array ..this should be my output..!! dynmic names in the char array..?? –  Gurjit Feb 22 '14 at 6:11
    
You mean, you want your standard output to be formatted like Java code? Why? Java is not a dynamic language, so you can't create arbitrary dynamic variables at run time.. if you intend to generate Java code and then execute it, you'd need to compile it first.. not sure I understood you correctly! –  Таня Т. Feb 22 '14 at 6:15

I think this is what you are looking for:

private static char[] stringArrayToCharArray (String[] words) {
    int arrayLength = 0;

    // Calculate the total size of the char array by adding the strings sizes
    for (final String word : words)
        arrayLength += word.length();

    // Create the char array
    char[] charArray = new char[arrayLength];

    int arrayIndex = 0;

    // Fill the array
    for (final String word : words)
        for (int i=0; i<word.length(); i++)
            charArray[arrayIndex++] = word.charAt(i);

    return charArray;           
}

Here is how to use it:

String[] strs = {"test", "other"};
char[] chars = stringArrayToCharArray(strs);
share|improve this answer

What you want is to put every character within the string array into the char array????.

If it is, you have several ways to do this.

To avoid using an array list (increases its capacity dinamycally), you will have to know the amount of character inside each index of you string array, go through each index adding the amount of character to a variable that you previouly had declared (lets called sumChar), then create the char array with a capacity equals to sumChar char [] Carray=new chat[sumChar].

then do something like this int indexPos=0;

for (int i=0;i<names.length;i++)
{
   for (int c=0;c<names[i];length;c++)
    {
          Carray[indexPos++]=names[i].charAt(c);
    }
}

that's all, the code above I made it without tested it , so any problem feel free to ask

share|improve this answer

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.