0% found this document useful (0 votes)
9 views20 pages

Passing Arrays to Java Methods

Uploaded by

ediludemissie2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Passing Arrays to Java Methods

Uploaded by

ediludemissie2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Passing Arrays to

Methods
Hui Chen
Department of Computer & Information Science
Brooklyn College

4/4/2024 CUNY | Brooklyn College 1


Objectives
• To develop and invoke methods with array
arguments and return values (§§7.6–7.8).
• To copy contents from one array to another (§7.5)
• To define a method with a variable-length
argument list (§7.9).

4/4/2024 CUNY | Brooklyn College 2


Passing Arrays to Methods
public static void printArray(int[] array) {
for (int i = 0; i < [Link]; i++) {
[Link](array[i] + " ");
}
}

Invoke the method

int[] list = {3, 1, 2, 6, 4, 2};


printArray(list);

Invoke the method


printArray(new int[]{3, 1, 2, 6, 4, 2});

Anonymous array
4/4/2024 CUNY | Brooklyn College 3
Anonymous Array
• The statement
printArray(new int[]{3, 1, 2, 6, 4, 2});
• creates an array using the following syntax:
new dataType[]{literal0, literal1, ..., literalk};
• There is no explicit reference variable for the array.
Such array is called an anonymous array

4/4/2024 CUNY | Brooklyn College 4


Pass By Value
• Java uses pass by value to pass arguments to a
method.
• However, there are important differences between
passing a value of variables of primitive data types
and passing arrays.

4/4/2024 CUNY | Brooklyn College 5


Pass by Value: Primitive Data Types
• For a parameter of a primitive type value, the
actual value is passed.
• Changing the value of the local parameter inside
the method does not affect the value of the
variable outside the method.

4/4/2024 CUNY | Brooklyn College 6


Pass by Value: Array
• For a parameter of an array type, the value of the
parameter contains a reference to an array
• This reference is passed to the method.
• Any changes to the array that occur inside the
method body will affect the original array that was
passed as the argument.

4/4/2024 CUNY | Brooklyn College 7


Pass by Value: Example
public class Test {
public static void main(String[] args) {
int x = 1; // x represents an int value
int[] y = new int[10]; // y represents an array of int values
m(x, y); // Invoke m with arguments x and y
[Link]("x is " + x);
[Link]("y[0] is " + y[0]);
}
public static void m(int number, int[] numbers) {
number = 1001; // Assign a new value to number
numbers[0] = 5555; // Assign a new value to numbers[0]
}
}

4/4/2024 CUNY | Brooklyn College 8


Call Stack

• When invoking m(x, y), the values of x and y are passed to number and
numbers.
• Since y contains the reference value to the array, numbers now contains
the same reference value to the same array

4/4/2024 CUNY | Brooklyn College 9


Call Stack

• When invoking m(x, y), the values of x and y are passed to


number and numbers.
• Since y contains the reference value to the array, numbers
now contains the same reference value to the same array
4/4/2024 CUNY | Brooklyn College 10
Heap Heap

The arrays are


5555 stored in a
0 heap.
Space required for the
main method
int[] y: reference
int x: 1 0

• The JVM stores the array in an area of memory,


called heap, which is used for dynamic memory
allocation where blocks of memory are allocated
and freed in an arbitrary order
4/4/2024 CUNY | Brooklyn College 11
Questions?

4/4/2024 CUNY | Brooklyn College 12


Example Problem. Passing Arrays vs.
Passing Primitive Data Types
• Compare swap(int n1, int n2) and
swapFirstTwoInArray(int[] array)
Stack Heap Stack
Space required for the
Space required for the swapFirstTwoInArray
swap method method
n2: 2 int[] array reference
n1: 1

Space required for the Space required for the


main method main method
int[] a reference int[] a reference
a[1]: 2
a[0]: 1
Invoke swap(int n1, int n2). Invoke swapFirstTwoInArray(int[] array).
The primitive type values in The arrays are The reference value in a is passed to the
a[0] and a[1] are passed to the stored in a swapFirstTwoInArray method.
swap method. heap.
4/4/2024 CUNY | Brooklyn College 13
Questions?

4/4/2024 CUNY | Brooklyn College 14


Returning an Array from a Method
public static int[] reverse(int[] list) {
int[] result = new int[[Link]];

for (int i = 0, j = [Link] - 1;


i < [Link]; i++, j--) {
result[j] = list[i];
}
list
return result;
} result

int[] list1 = {1, 2, 3, 4, 5, 6};


int[] list2 = reverse(list1);

4/4/2024 CUNY | Brooklyn College 15


Questions

4/4/2024 CUNY | Brooklyn College 16


Problem. Counting Occurrence of
Each Letter
• Generate 100 lowercase letters randomly and
assign to an array of characters.
• Count the occurrence of each letter in the array.

4/4/2024 CUNY | Brooklyn College 17


Variable-Length Arguments and
Array
• You can pass a variable number of arguments of the
same type to a method

4/4/2024 CUNY | Brooklyn College 18


Example
public class VarArgsDemo {
public static void main(String[] args) {
printMax(34, 3, 3, 2, 56.5);
printMax(new double[]{1, 2, 3});
}

public static void printMax(double... numbers) {


if ([Link] == 0) {
[Link]("No argument passed");
return;
}

double result = numbers[0];

for (int i = 1; i < [Link]; i++)


if (numbers[i] > result)
result = numbers[i];

[Link]("The max value is " + result);


}
}

4/4/2024 CUNY | Brooklyn College 19


Questions?

4/4/2024 CUNY | Brooklyn College 20

You might also like