-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture21ChangeReference.java
More file actions
52 lines (49 loc) · 1.33 KB
/
Lecture21ChangeReference.java
File metadata and controls
52 lines (49 loc) · 1.33 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
47
48
49
50
51
52
import java.util.Scanner;
/**
* Lecture 21
* calling methods
* pass arguments by value
*
* @author PMCampbell
* @version 2020-12-01
*/
public class Lecture21ChangeReference {
public static void main(String[] args) {
double[] z = { 1.0, 2.0};
double[] q = { 1.0, 3.0, 4.0 };
printArray(z, "Array z, before calling foo() ");
foo(q , z);
printArray(z, "Array z, after calling foo() ");
}
/**
* 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[]) {
double q[] = new double[3];
y = new double[] {5,10,15,20,25 };
y[3] = 55;
// now y is pointing to a new array,
// not the same one it came in with
}
/**
* display contents of an array with {}
* from lab 19
*
* @param array array to be printed
* @param header header to print before array contents
*/
public static void printArray(double array[], String header) {
System.out.println(header);
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(" }");
}
}