Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String array -> char array

Status
Not open for further replies.

brownie124

Programmer
Sep 19, 2002
61
US
Hi,

I have a 2 dimensional String array, something like this:
Code:
String foostring[][] = { {"hello"}, {"there"} };

I am trying trying to get those values into 2 dimensional character array. I have it define in my class as:
Code:
char foochar[][];

I am having a hard time getting anything into foochar. How would I do this?

Thanks in advance.

- Michael
 
Putting a 2 dimensional String array in a 2 dimensional char array is impossible. Your char array needs to be 1 dimension bigger than the String array. So I'll give you the code to put a 1 dimensional String array in a 2 dimensional char array. [This is probably the way you wanted it anyway].
Code:
public class StringExample {

  public StringExample() {
  }

  public void doIt() {
    // 1 dimensional String array !!!
    String foostring[] = { "hello", "there" };
    char fooArray[][] = new char[foostring.length][];
    for (int i=0; i<foostring.length; i++) {
      System.out.println(&quot;Converting foostring[&quot; + i + &quot;] = &quot; + foostring[i]);
      fooArray[i] = foostring[i].toCharArray();
      for (int j=0; j<fooArray[i].length; j++) {
        System.out.println(&quot;  fooArray[&quot; + i + &quot;][&quot; + j + &quot;] = &quot; + fooArray[i][j]);
      }
    }
  }

  public static void main(String args[]) {
    StringExample stringExample=new StringExample();
    stringExample.doIt();
  }

}
 
Thank you so much! I really do want a 2 dimensional String array into a 2 dimensional (3 dimensional) character array; I just wrote the example incorrectly. But the example you gave me was perfect, thanks.

- Michael
 
I am doing that because it was my understanding that because a String is an immutable object, I could not modify it. Here is the scenario:
I have a 2 dimensional array of Strings. I need to break up the data into 2 different arrays. I tried doing something like this:

Code:
String array1[][];
String array2[][];
String mydata[][] = {
                        {new String(&quot;data1&quot;), new String(&quot;data2&quot;), new String(&quot;data3&quot;)},
                        {new String(&quot;data4&quot;), new String(&quot;data5&quot;), new String(&quot;data6&quot;)}
                    }

This is just an example, but I need to put my first record which is data1 - data3 into array1[][] and my second record, data4 - data6 into array2[][]. In addition, I need to do sorts on the various columns of data. I just didn't think I could do all that with Strings because they are immutable.

Make sense?

Thanks,
- Michael
 
You can't modify a string per say, but you can create new Strings and StringBuffers from existing String data, essentially modifying it without the need for a character array.
 
I will look into the StringBuffers. That might be something that will work.

Thanks,
- Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top