Results 1 to 11 of 11
Thread: toCharArray() How it works ?
- 08-02-2011, 07:43 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
- 08-02-2011, 07:52 PM #2
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.
- 08-02-2011, 08:15 PM #3
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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); } }
- 08-02-2011, 08:26 PM #4
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; }
- 08-02-2011, 09:18 PM #5
It does NOTHING to the String. Strings are immutable.What it does to the invoking string?
- 08-03-2011, 12:32 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It basically can be thought of as looking like this
The exact code is going to different, but somewhat similar.Java Code:public class String{ char[] info; public char[] toCharArray(){ return Arrays.copyOf(info, info.length); } }
- 08-03-2011, 09:36 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Well, the exact code (from the src for String in the JDK) is:
where getChars does:Java Code:public char[] toCharArray() { char result[] = new char[count]; getChars(0, count, result, 0); return result; }
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.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); }
- 08-03-2011, 10:29 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I was being a bit lazy I suppose.
- 08-03-2011, 10:48 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Sorry, that last bit was directed at the OP...oops...
- 08-04-2011, 10:57 PM #10
You can download the source code for the String class.
java.lang: String.java
The toCharArray() method is actually very simple.
- 08-05-2011, 09:19 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
How JVM works in linux box ?
By vinothjava in forum Advanced JavaReplies: 14Last Post: 05-06-2011, 12:02 PM -
Tell me how this loop works.
By hydride in forum New To JavaReplies: 6Last Post: 05-13-2010, 04:50 AM -
String.contains works in jdk 6, not in 1.4.2
By ScottThornley in forum New To JavaReplies: 1Last Post: 04-16-2010, 03:04 PM -
String toCharArray problem - static context
By Grendel0 in forum New To JavaReplies: 10Last Post: 03-19-2010, 01:28 PM -
Anyone know how GroupLayout works?
By ProgrammingPup in forum Advanced JavaReplies: 5Last Post: 12-01-2009, 11:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks