I wish to do the something like the following:
public static int[] plusOneLengthFour(int[] arr) {
return {arr[0]+1,arr[1]+1,arr[2]+1,arr[3]+1};
}
But at compile time, I get the following error:
TestClass.java:5: error: illegal start of expression
return {arr[0]+1,arr[1]+1,arr[2]+1,arr[3]+1};
What's wrong here?
You just need to add new int[] to your return statement to compile it. It doesn't know what you want if you just start with curly braces. Note that this will return a new array, while the original arr will have the same values.
i.e.,
return new int[] {arr[0]+1,arr[1]+1,arr[2]+1,arr[3]+1};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With