-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTestPassArray.java
49 lines (44 loc) · 1.25 KB
/
TestPassArray.java
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
package chapter_seven.samples;
/**
* Listing 7.3 TestPassArray.java
*/
public class TestPassArray
{
/**
* Main method
*/
public static void main(String[] args)
{
int[] a = {1, 2};
// Swap elements using the swap method
System.out.println("Before invoking swap");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
swap(a[0], a[1]);
System.out.println("After invoking swap");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
// Swap elements using the swapFirstTwoInArray method
System.out.println("Before invoking swapFirstTwoInArray");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
swapFirstTwoInArray(a);
System.out.println("After invoking swapFirstTwoInArray");
System.out.println("array is {" + a[0] + ", " + a[1] + "}");
}
/**
* Swap two variables
*/
public static void swap(int n1, int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
/**
* Swap the first two elements in the array
*/
public static void swapFirstTwoInArray(int[] array)
{
int temp = array[0];
array[0] = array[1];
array[1] = temp;
}
}