Results 1 to 11 of 11
  1. #1
    Austin is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default toCharArray() How it works ?

    Guys , i wud like to know how exactly toCharArray works in java. Like an algorithm of its function. I dont want its application, just the process behind it. What it does to the invoking string?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    the toCharArray method returns an Object of type charArray.
    For instance:

    String message = "abc";

    char[] convertedString = message.toCharArray();

    This will yield you a char array with 3 elements.
    The 0th element will be: convertedString[0]='a';
    The 1st element will be: convertedString[1]='b';
    The 2nd element will be: convertedString[2]='c';

    So most of the time you will be using the toCharArrayMethod to take a String and get an array of characters as output.
    Does that help?

    Maybe look at the javaDoc for a String if you need specifics, there are ton's of examples on the web.
    Last edited by sehudson; 08-02-2011 at 07:55 PM.

  3. #3
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Java Code:
    public char[] toCharArray(String input){
    char array = new char[input.length()];
    for (int i = 0; i <= input.length() ; i++) {
    array[i] = input.charAt(i);
    }
    }

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    Quote Originally Posted by crikey View Post
    Java Code:
    public char[] toCharArray(String input){
    char array = new char[input.length()];
    for (int i = 0; i <= input.length() ; i++) {
    array[i] = input.charAt(i);
    }
    }
    I think you have a few issues, for one you are missing the return statement. Hows this:


    Java Code:
        public char[] toCharArray(String input) {
            int size = input.length();
            char[] returnedArray = new char[size];
            for (int i = 0; i < size; i++) {
                returnedArray[i] = input.charAt(i);
            }
            return returnedArray;
        }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    What it does to the invoking string?
    It does NOTHING to the String. Strings are immutable.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It basically can be thought of as looking like this
    Java Code:
    public class String{
      char[] info;
      public char[] toCharArray(){
        return Arrays.copyOf(info, info.length);
      }
    }
    The exact code is going to different, but somewhat similar.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Well, the exact code (from the src for String in the JDK) is:
    Java Code:
        public char[] toCharArray() {
    	char result[] = new char[count];
    	getChars(0, count, result, 0);
    	return result;
        }
    where getChars does:
    Java Code:
        public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
            if (srcBegin < 0) {
                throw new StringIndexOutOfBoundsException(srcBegin);
            }
            if (srcEnd > count) {
                throw new StringIndexOutOfBoundsException(srcEnd);
            }
            if (srcBegin > srcEnd) {
                throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
            }
            System.arraycopy(value, offset + srcBegin, dst, dstBegin,
                 srcEnd - srcBegin);
        }
    Which you could have found for yourself just by looking in the source code...took me less than a minute. In fact took me longer to type this.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I was being a bit lazy I suppose.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Sorry, that last bit was directed at the OP...oops...

  10. #10
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Austin View Post
    Guys , i wud like to know how exactly toCharArray works in java. Like an algorithm of its function. I dont want its application, just the process behind it. What it does to the invoking string?
    You can download the source code for the String class.

    java.lang: String.java

    The toCharArray() method is actually very simple.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

Similar Threads

  1. How JVM works in linux box ?
    By vinothjava in forum Advanced Java
    Replies: 14
    Last Post: 05-06-2011, 12:02 PM
  2. Tell me how this loop works.
    By hydride in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 04:50 AM
  3. String.contains works in jdk 6, not in 1.4.2
    By ScottThornley in forum New To Java
    Replies: 1
    Last Post: 04-16-2010, 03:04 PM
  4. String toCharArray problem - static context
    By Grendel0 in forum New To Java
    Replies: 10
    Last Post: 03-19-2010, 01:28 PM
  5. Anyone know how GroupLayout works?
    By ProgrammingPup in forum Advanced Java
    Replies: 5
    Last Post: 12-01-2009, 11:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •