Results 1 to 12 of 12
Thread: Array in Java '\0' ?
- 02-12-2013, 10:16 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
Array in Java '\0' ?
Does character array in java has '\0' by default in the end?
For example:
char[] ch = {'t', 'h', 'e', ' ', 'd', 'o', 'g', ' ', ' ', ' ', ' ', ' ', ' '};
The Length of above character arrays is 13. So, if I want to start from backwwards then can I initialize a variable calles, newlength = '\0' and start working
backwards?
Please clarify.
Thanks
- 02-12-2013, 10:19 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Array in Java '\0' ?
Java is not C nor C++; Java doesn't use the '\0' character to indicate the end of string ('end of char array' actually in C and C++). Java keeps a 'length' field for arrays so it can do bound checking behind the scenes.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-12-2013, 10:30 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
Re: Array in Java '\0' ?
Thanks for your reply. Actually, I'm confused with the following program which has "str[newLength] = '\0';" . Why '\0' is mentioned here?
The following program is replacing all the spaces in the strigng with '%20'.
Please explain.
================================================== ================================================
public class Test {
public void replaseSpaces(char[] str, int length) {
int spaceCount = 0, newLength = 0, i = 0;
for(i = 0; i < length; i++) {
if (str[i] == ' ')
spaceCount++;
}
newLength = length + (spaceCount * 2);
str[newLength] = '\0';
for(i = length - 1; i >= 0; i--) {
if (str[i] == ' ') {
str[newLength - 1] = '0';
str[newLength - 2] = '2';
str[newLength - 3] = '%';
newLength = newLength - 3;
}
else {
str[newLength - 1] = str[i];
newLength = newLength - 1;
}
}
System.out.println(str);
}
public static void main(String[] args) {
Test tst = new Test();
char[] ch = {'t', 'h', 'e', ' ', 'd', 'o', 'g', ' ', ' ', ' ', ' ', ' ', ' '};
int length = 6;
tst.replaseSpaces(ch, length);
}
}
================================================== ================================================== =
- 02-12-2013, 10:53 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Array in Java '\0' ?
That's the 'mechanical' way of doing things in Java; the following snippet replaces spaces with "%20" in a String:
kind regards,Java Code:String myString= "the dog and the cat"; myString= myString.replaceAll("\\s", "%20"); System.out.println(myString);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-12-2013, 10:59 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
Re: Array in Java '\0' ?
Thanks for your answer but could you also explain my question pertaining to my program mentioned above?
- 02-12-2013, 11:04 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 02-12-2013, 11:18 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Array in Java '\0' ?
Ask whoever wrote it.
Because I suspect they simply took a C program and rewrote almost word for word into Java, without actually knowing what they were doing.Please do not ask for code as refusal often offends.
- 02-12-2013, 11:35 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
Re: Array in Java '\0' ?
I didn't mean explanation of my program. The program is running perfectly fine:
I repeat my question once again pertaining to the program I mentioned above:
"Actually, I'm confused with the following program which has "str[newLength] = '\0';" . Why '\0' is mentioned here? "
Please explain.
I hope this clarifies my question.
- 02-12-2013, 12:51 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Array in Java '\0' ?
And as I said, ask whoever wrote it about why they thought it was useful.
As far as I can make out it's not.Please do not ask for code as refusal often offends.
- 02-12-2013, 01:56 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 02-13-2013, 12:38 AM #11
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
Re: Array in Java '\0' ?
Actually, the value of length should be 7 instead of 6. Then you will get "the%20dog" as your output.
As we discussed that java doesn't' support "\0", then how come this program is not throwing any error? How come it's running fine even though chances are there that someone who wrote the program copied it from C language.
Please comment on this.
Thanks
- 02-13-2013, 10:12 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Bit Array to Byte Array in java
By Umesh Joshi in forum New To JavaReplies: 1Last Post: 11-30-2012, 08:24 AM -
Java 2D Array
By The Dark Dragon in forum New To JavaReplies: 3Last Post: 11-27-2011, 08:00 AM -
`java array
By shane123 in forum New To JavaReplies: 18Last Post: 03-22-2010, 12:42 AM -
How to transfer 1D array in JAVA to 3D array in C
By fishwater00 in forum New To JavaReplies: 0Last Post: 07-31-2009, 06:24 PM -
how to convert a Java array to a java stack?
By pompeez in forum New To JavaReplies: 2Last Post: 08-13-2007, 02:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks