-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem9.java
More file actions
35 lines (29 loc) · 1.03 KB
/
Problem9.java
File metadata and controls
35 lines (29 loc) · 1.03 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
/*The method receives two parameters, an integer array A and an integer variable b. The method returns a new
array (do not change A) that is identical to A except that all cells that contain the value stored in the variable b
are no longer present. */
public class Problem9 {
public static void main(String[] args) {
int[] A = {6, 103, 77, 49, 0, 83, 77, 77, 444, 444};
int b = 77;
int[] result = newSmallerArray(A, b);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
} // end of main method
public static int[] newSmallerArray(int[] A, int b) {
int count = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] != b) {
count++;
}
}
int[] copy = new int[count];
count = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] != b) {
copy[count++] = A[i];
}
}
return copy;
} // end of method new smaller array
}