An example of passing array as parameters to methods
// the program demonstrates passing an array as a parameter
// to a method
public class ArrayParameter {
public static void main( String [] args) {
int [] numbers = {1, 3, 4, 7, 9};
// the reference to the array is passed to
// the method
addOne(numbers);
// the new elements of the array are printed
print(numbers);
int [] morenumbers = {1, 7, 8, -1, 6, 6 , 9, 3 , -8, 0, 55};
addOne(morenumbers);
print(morenumbers);
}
// the elements of the array are changed in the method
public static void addOne(int [] items) {
for (int i = 0; i < items.length; ++i) {
items[i]++;
}
}
public static void print(int [] items) {
// print array elements all on one line
for (int i = 0; i < items.length; ++i) {
System.out.print(items[i] + " ");
}
System.out.println();
}
}
This is an example from CSci 2101 course.