-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtils.java
More file actions
104 lines (93 loc) · 3.04 KB
/
ArrayUtils.java
File metadata and controls
104 lines (93 loc) · 3.04 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Jessica Yang
// APCS1 pd9
// HW27 -- Objectify Your Array Tools
// 2015-11-09
public class ArrayUtils {
// populates an existing array with randomly generated ints within a certain range
public static Object[] populate(Object[] array, int lower, int upper) {
for (int x = 0; x < array.length; x++) {
array[x] = (int)(lower + Math.random() * (upper - lower));
}
return array;
}
// returns a String version of an array of ints
public static String arrayToStr(Object[] array) {
String strArray = "";
for (int x = 0; x < array.length; x++) {
strArray += array[x] + ", ";
}
return strArray.substring(0, strArray.length() - 2);
}
// helper fxn for recursion to make new array with all elements but the first
public static Object[] cdr(Object[] a) {
Object[] newA = new Object[a.length - 1];
for (int x = 0; x < newA.length; x++) {
newA[x] = a[x + 1];
}
return newA;
}
// a linear search function that will return the index of the first occurrence of a target in an existing array, or -1 if not found (iterative)
public static int linSearch(Object[] a, Object target) {
for (int x = 0; x < a.length; x++) {
if (a[x] == target) {
return x;
}
}
return -1;
}
// a linear search function that will return the index of the first occurrence of a target in an existing array, or -1 if not found (recursive)
public static int linSearchR(Object[] a, Object target) {
if (freq(a, target) == 0) {
return -1;
}
if (a[0] == target) {
return 0;
}
return 1 + linSearchR(cdr(a), target);
}
// a frequency function that will return the number of occurrences of a target in an existing array (iterative)
public static int freq(Object[] a, Object target) {
int occ = 0;
if (linSearch(a, target) == -1) {
return occ;
}
for (int x = 0; x < a.length; x++) {
if (a[x] == target) {
occ += 1;
}
}
return occ;
}
// a frequency function that will return the number of occurrences of a target in an existing array (recursive)
public static int freqRec(Object[] a, Object target) {
int occ = 0;
if (linSearch(a, target) == -1) {
return occ;
} else {
if (a[0] == target) {
occ += 1;
}
return freqRec(cdr(a), target) + occ;
}
}
// main method for testing
public static void main(String[] args) {
Object[] intArr = new Integer[5];
intArr = populate(intArr, 0, 100);
System.out.println("\n======INT ARRAY=====");
for (int i = 0; i < intArr.length; i++) {
System.out.println("intArr[" + i + "] = " + intArr[i]);
}
System.out.println("arrayToStr(intArr):\t" + arrayToStr(intArr) +
"\nlinSearch(intArr,6):\t" + linSearch(intArr, 6) +
"\nfreq(intArr, 7):\t" + freq(intArr, 7) + "\n");
Object[] strArr = {"1", "23", "45", "6", "789"};
System.out.println("=====STR ARRAY=====");
for (int i = 0; i < strArr.length; i++) {
System.out.println("strArr[" + i + "] = " + strArr[i]);
}
System.out.println("arrayToStr(strArr):\t" + arrayToStr(strArr) +
"\nlinSearch(strArr, \"6\"):\t" + linSearch(strArr, "6") +
"\nfreq(strArr, \"7\"):\t" + freq(strArr, "7") + "\n");
}
}