/* ArrayElementArguments.java
Craig Persiko
Sample program for CS 111A
Demonstrates how to pass an element of an array into a method.
*/
class ArrayElementArguments
{
public static void main(String[] args)
{
int i;
int[] a = new int[4];
a[0] = 1;
for(i=0; i<a.length-1; i++)
a[i+1] = foo(a[i]);
/* Notice above loop condition is i<3
because [i+1] is used as an array index.
The array index must always be less than the array size.
*/
for(i=0; i<4; i++)
System.out.println(a[i]);
}
// returns 2 times x
public static int foo(int x)
{
return x * 2;
}
}
/* Output:
[cpersiko@milhouse ~]$ java ArrayElementArguments
1
2
4
8
[cpersiko@milhouse ~]$
*/
syntax highlighted by Code2HTML, v. 0.9