public class MainClass public static int addFirst int arr int el int a

 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
public class MainClass {
public static int[] addFirst(int[] arr, int el) {
int[] arr1 = new int[arr.length + 1];
arr1[0] = el;
for (int i = 1; i < arr1.length; i++) arr1[i] = arr[i - 1];
return arr1;
}
public static int[] removeFirst(int[] arr, int el) {
int[] arr1 = new int[arr.length - 1];
for (int i = 1; i < arr.length; i++) arr1[i - 1] = arr[i];
return arr1;
}
public static int[] addToK(int[] arr, int el, int k) {
int[] arr1 = new int[arr.length + 1];
for (int i = 0; i < k; i++) arr1[i] = arr[i];
arr1[k] = el;
for (int i = k + 1; i < arr1.length; i++) arr1[i] = arr[i-1];
return arr1;
}
public static int[] removeFromK(int[] arr, int k) {
int[] arr1 = new int[arr.length - 1];
for (int i = 0; i < k; i++) arr1[i] = arr[i];
for (int i = k + 1; i < arr.length; i++) arr1[i-1] = arr[i];
return arr1;
}
public static int[] addToKonec(int[] arr, int el) {
int[] arr1 = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) arr1[i] = arr[i];
arr1[arr.length] = el;
return arr1;
}
public static int[] removeFromKonec(int[] arr) {
int[] arr1 = new int[arr.length - 1];
for (int i = 0; i < arr.length - 1; i++) arr1[i] = arr[i];
return arr1;
}
public static int[] removeAllEl(int[] arr, int el) {
int len = 0;
for (int i = 0; i < arr.length; i++) if(arr[i] == el)len++;
int[] arr1 = new int[arr.length - len];
for (int i = 0, j = 0; i < arr.length; ) {
if (arr[i] == el) i++;
else { arr1[j] = arr[i]; j++; i++; }
}
return arr1;
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
printArray(arr);
arr[7] = 4;
arr = removeAllEl(arr, 4);
printArray(arr);
}
}