-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathArrayFunctions.java
More file actions
107 lines (96 loc) · 3.21 KB
/
Copy pathArrayFunctions.java
File metadata and controls
107 lines (96 loc) · 3.21 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
105
106
107
/*
* Created by IntelliJ IDEA.
* User: divyanshb
* Date: 09/01/20
* Time: 10:03 AM
*/
package array;
public class ArrayFunctions {
/**
* This method will insert an integer value in an integer array at the last index.
* If the array is already full, it will do nothing.
*
* @param array the integer array in which the value is to be inserted
* @param value the value that is to be inserted
*/
public void insertValue(int[] array, int value) {
if (array[array.length - 1] == 0) {
array[array.length - 1] = value;
}
}
/**
* This method is the overloaded form of the {@code insertValue()} method which
* lets the user tell the index at which the value is to be inserted.
* If the index already has a value, this method will override it.
*
* @param array the integer array in which the value is to be inserted
* @param value the value that is to be inserted
* @param index the index at which the value is to be inserted
*/
public void insertValue(int[] array, int value, int index) {
array[index] = value;
}
/**
* This method will delete a value inside the array.
* It will first search for the value inside the array, if the value exists,
* then the array value will be deleted, and the method will return {@code true}.
* If the value does not exist inside the array, then the method will return {@code false}.
*
* @param array the integer array in which the value is to be deleted.
* @param value the value which is to be deleted.
* @return {@code true} if the value was deleted; {@code false} if the value was not present.
*/
public boolean deleteValue(int[] array, int value) {
boolean response = false;
int index = searchValue(array, value);
if (index != -1) {
shiftElements(array, index);
response = true;
}
return response;
}
public void shiftElements(int[] array, int index) {
for (int i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
insertValue(array, 0);
}
/**
* This method will delete an integer value from the integer array based on
* the given index.
*
* @param index the index at which the value is to be deleted
* @param array the integer array
*/
public void deleteValue(int index, int[] array) {
// boolean response = false;
shiftElements(array, index);
// return response;
}
/**
* This method will search for an integer value inside an integer array.
*
* @return integer
*/
public int searchValue(int[] array, int value) {
int response = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
response = i;
break;
}
}
return response;
}
/*
* This method will sort the values of the integer array in an increasing order.
*
* */
public void sortValues(int[] array) {
}
/*
* This method will traverse the array and print all the values.
* */
public void printArrayValues(int[] array) {
}
}