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 originalarrwill have the same values.i.e.,