What I am trying to do is convert this C++ function to Java:
void print(int x[], int l)
{
if (l != 0) {
cout << x[0] << ",";
print(x+1,l-1);
}
}
This is what I am trying to do, but it won’t work:
void print(int x[])
{
if (x.length != 0) {
cout << x[0] << ",";
print(x + 1); //<--- ERROR!
}
}
Does anybody have any ideas?
Java does not have pointers in the same way as C++. That is, you can’t “move” the array pointer
xfurther along the array. What I would suggest is passing a starting index to your function something like this: