-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture21Pass.java
More file actions
46 lines (43 loc) · 1.08 KB
/
Lecture21Pass.java
File metadata and controls
46 lines (43 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;
/**
* Lecture 21
* calling methods
* pass arguments by value
*
* @author PMCampbell
* @version 2020-12-01
*/
public class Lecture21PassReference {
public static void main(String[] args) {
double[] z = new double[] { 1.0, 2.0};
double[] q = new double[] { 1.0, 3.0, 4.0 };
printArray(z);
foo(q , z);
printArray(z);
}
/**
* fake method to illustrate
* that an array as an argument is a pointer
*
* we cannot change the pointer itself
* but we can change the content of the array
*/
public static void foo(double x[], double y[]) {
y[0]= 5;
}
/**
* display contents of an array with {}
* from lab 19
*
* @param array array to be printed
*/
public static void printArray(int array[]) {
System.out.print("{ ");
for(int i=0;i <array.length; i++) {
if (i == array.length-1) {
System.out.print(array[i]);
}else { System.out.print(array[i] + ","); }
}
System.out.println(" }");
}
}