Java stores lists of values as arrays. An array names a contiguous group of
related memory locations. Arrays in Java are objects and space for them must be allocated
with the new
operator.
double v[] = new double[10];
v[4] = 32.0;
v
is determined by the expression
v.length
The elements of an array can be allocated and initialized by an initializer list:
int nval[] = { 10, 30, 15, 5 };
double [] getArray()
returns an array of doubles.void passArray(double [] arg)
receives an array of doubles.
public class Arrays { public static void main( String args[] ) { int in[] = MethodA(); int inx[] = in; System.out.println("The two arrays are" + (inx==in?" ":" NOT ") + "equal"); System.out.println("Array inx.toString() = " + inx.toString() ); System.out.println("Array in.toString() = " + in.toString() ); MethodB(in); in[3] = 44; System.out.println("Change in[3] to 44, inx[3] is " + inx[3]); } public static int [] MethodA() { int n[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; System.out.println("MethodA.n.toString() = " + n.toString() ); return n; } public static void MethodB(int [] inp) { System.out.println("Input array has " + inp.length + " elements"); } }
Maintained by John Loomis, last updated 1 June 2000