Java Code Example: passing array to method

This code is a Java program that demonstrates the use of methods with arrays. The doubles method takes an array of integers as an argument and doubles each value in the array. The main method declares an array of integers with values 1, 2, 3, 4, 5, 6, 7, calls the doubles method to double the values in the array, and then prints the values of the array.

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] + " ");
        }
    }
}
Output
values from array doubled: 
2 4 6 8 10 12 14