PASSING ARRAYS TO A METHOD
PASSING A ONE-DIMENSIONAL ARRAY
TO A METHOD
- A single array element is passed to
a method by value, the same as any primitive variable. Only a copy of the
element is made, and the method does not alter the value of the element
directly.
Example
public class Test
{ public static void main(String[ ] args)
{ double grades[ ] = {85.0, 67.0, 90.0};
display(grades[2]);
System.out.println(grades[2]);
}
public static void display(int x)
{ x = x + 10.0;
System.out.println(x);
}
}
Output
100.0
90.0
- An entire array is passed to a
method by reference, so the method may directly modify elements of the
array.Because an array is an object, when the entire array is passed as a
parameter, a copy of the reference to the original array is passed. The
method can change an element of the array permanently because it is
referring to the original element value.
Example
public class Test
{ public static void main(String[ ] args)
{ double grades[ ] = {85.0, 67.0, 90.0};
add10(grades);
for(i =0; i <grades.length; i++)
System.out.println(grades[i] + " " );
}
public static void add10(double [ ] x)
{ for(int i =0; i < x.length;i++)
{ x[i] = x [i] + 10.0;
}
}
}
Output
95.0 77.0 100.0
RETURNING ONE-DIMENSIONAL ARRAY
FROM THE METHOD
A method can return an one-dimensional array.
Syntax: return arrayname;
Example
class Test
{ public int [ ] getArray( )
{int array[ ] = {1,8,5};
return array;
}
}
public class TestArray
{ public static void main(String[ ] args)
{ Test x = new Test( );
int y[ ] = x.getArray( );
}
}
PASSING A TWO-DIMENSIONAL ARRAY
TO A METHOD
- A single array element is passed to
a method by value, the same as any primitive variable. Only a copy of the
element is made, and the method does not alter the value of the element
directly.
Example
public class Test
{ public static void main(String[ ] args)
{ double grades [3][4] = { { 45.0, 89.0, 90.0, 67.0}, { 54.0, 75.0,
34.0, 99.0}, {88.0, 92.0, 10.0, 56.0} };
display(grades[2][1]);
}
public static void display(double d)
{ System.out.println (d);
}
}
- An entire array is passed to a
method by reference, so the method may directly modify elements of the
array.Because an array is an object, when the entire array is passed as a
parameter, a copy of the reference to the original array is passed. The
method can change an element of the array permanently because it is
referring to the original element value.
Example
public class Test
{ public static void main(String[ ] args)
{ double grades [3][4] = { { 45.0, 89.0, 90.0, 67.0}, { 54.0,
75.0, 34.0, 99.0}, {88.0, 92.0, 10.0, 56.0} };
display(grades);
}
public static void display(double g[3][4])
{ for (int row =0; row <g.length; row++)
{ for (int col = 0; col <g[row].length; col++)
{ System.out.println(g[row][col] + " ");
}
System.out.println( );
}
}
}
RETURNING TWO-DIMENSIONAL ARRAY
FROM THE METHOD
A method can return a two-dimensional array.
Syntax: return arrayname;
Example
class Test
{ public double [ ] [ ] getGrades( )
{double grades [3][4] = { { 45.0, 89.0, 90.0, 67.0}, { 54.0, 75.0, 34.0,
99.0}, {88.0, 92.0, 10.0, 56.0} };
return grades;
}
}
public class TestArray
{ public static void main(String[ ] args)
{ Test x = new Test( );
double y[ ][ ] = x.getGrades( );
}
}