Passing an Array to a Method
public
class
Passing
{
public
static void
main(String[]args)
{
int
myArray[] = {1,2,3,4,5};
System.out.println("This is the original array");
for(int
a=0;a<myArray.length;a++)
{
System.out.print(myArray[a]
+ " ");
}
System.out.println();
manipulateArray(myArray);
System.out.println("This is the array as modified by the method");
for
(int i = 0;
i<myArray.length;i++)
{
System.out.print(myArray[i]
+ " ");
}
}
public
static void
manipulateArray(int myArray[])
{
for(int
i=0;i<myArray.length;i++)
{
myArray[i] = 3*i - 1;
}
}
}
OUTPUT
This is
the original array
1 2 3 4 5
This is
the array as modified by the method
-1 2 5 8 11