-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture19CompareArrays.java
More file actions
56 lines (55 loc) · 1.54 KB
/
Lecture19CompareArrays.java
File metadata and controls
56 lines (55 loc) · 1.54 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
53
54
55
56
/**
* This class illustrates comparing 2 arrays
* lecture 19 exercise
*
* @author PMCampbell
* @version today
**/
import java.util.Scanner;
public class Lecture19CompareArrays {
public static void main(String[] args) {
int array1[] = {1,2,3, 1};
int array2[] = new int[4];
boolean equal = true;
array2[0] =1;
array2[1] =6;
array2[2] =3;
array2[3] =1;
System.out.println("compare array1 pointer to array2 pointer " + (array1 == array2));
for (int i = 0; i < array1.length; i++) {
if (array1[i] != array2[i]) {
equal = false;
System.out.println("debug: at i "+i+" array1 "+array1[i]+" array2 "+array2[i]);
}
}
if (equal) {
System.out.println("The array contents are equal");
} else {
System.out.println("The array contents are NOT equal");
}
// compare 1st element to other elements in the array
equal = false;
int zeroth = array1[0];
for (int i = 1; i<array1.length; i++) {
if (zeroth == array1[i]) {
equal = true;
}
}
if (equal) {
System.out.println("The 0th element of the array occurs more than once");
} else {
System.out.println("The 0th element of the array DOES NOT occur more than once");
}
equal = false;
for (int i = 0; i<array1.length; i++) {
if (array1[i] == array1[i+1]) {
equal = true;
}
}
if (equal) {
System.out.println("The 0th element of the array occurs more than once");
} else {
System.out.println("The 0th element of the array DOES NOT occur more than once");
}
}
}