Method for doubling the elements of an integer array. Due to the referent passing, the values of the array change in the main program.
public class MethodsArrayExample {
public static void doubles(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = 2 * array[i];
}
}
public static void main(String args[]) {
int[] myArray = { 1, 2, 3, 4, 5, 6, 7 };
doubles(myArray);
System.out.println("values from array doubled: ");
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
}
}
values from array doubled:
2 4 6 8 10 12 14