Reputation: 7844
I am having some difficulty in understanding how to write the below piece of code using String
or char[]
in Java.
void xyz(char *a, int startIndex, int endIndex)
{
int j;
for (j = startIndex; j <= endIndex; j++)
{
doThis((a+startIndex), (a+j));
xyz(a, startIndex+1, endIndex);
}
}
Here char *a
points to the starting location of the char name[]
The above are just some random functions, but I just want the logic of how to use char*
and character index char[]
in Java
Upvotes: 1
Views: 1880
Reputation: 15656
If your method modifies the values you cant use String as that is immutable, you can use StringBuilder instead.
If your methods already rely on char arrays and you need the offsets you can use a CharBuffer to wrap an array. It does not support String operations but supports views for sub ranges, which seems to be what you use in the doThis()
method.
Upvotes: 0
Reputation: 198033
Based on the rephrased question from the comment thread:
You cannot change the characters of a Java String
. If you need to modify a sequence of characters, use StringBuilder
, which supports setCharAt(int, char)
, insert(int, char)
, and append(char)
. You can use new StringBuilder(myString)
to convert a String
to a StringBuilder
, and stringBuilder.toString()
to convert back.
This is perfectly legit Java code -- it's not code smelly, it's just the way you work with mutable character sequences.
Upvotes: 3
Reputation: 6871
You can't do exactly that. Java is pass by reference only. You don't have access to memory location information, so you can't do arithmetic with it.
It looks like you are passing in a string for manipulation. You have several options to simulate that.
There's a hack that can also be used to circumvent pass by reference, but it's poor style except in special situations. Pass in an array of whatever you need to modify, so in this case an array of array of characters would allow you to set a new sub-array, and effectively acheive pass by reference, but try not to do this :)
Upvotes: 0
Reputation: 2251
A char* in C is, as you noted, pointing to the start of your character array (which is how C manages Strings).
In C the size of a char is one byte, and pointers always point to the start of a byte. Your C String is an array of characters, so adding 1 to a pointer moves the start of your string right by one character.
That means that the C code:
char *a;
// Set the String here
a = a + 1;
translates in Java to something like:
String a;
// Set the String here
a = a.substring(1);
or if you are using a char array:
char[] a;
// Set the array contents here
char[] copyTo = new char[a.length];
System.arraycopy(a, 1, copyTo, 0, a.length);
a = copyTo;
Java will be a bit more careful of protecting you that C will be though. For instance, if you have a zero length string, the C code has the potential to either segfault (crashing the application) or give you a gibberish string full of memory junk (then, eventually, crash the application), whereas the Java code will throw an exception (normally an IndexOutOfBoundsException) which you can, hopefully, handle cleanly.
Remember though, that String in Java are immutable. You cannot change them, you can only create new Strings. Fortunately, String has several built in functions which allow you to do a lot of the standard actions, like replace part of the String with another and return the result. A character array is mutable, and you can change the characters within them, but you will lose a lot of the nice benefits you get from using the proper String class.
Upvotes: 3