File tree Expand file tree Collapse file tree 2 files changed +34
-3
lines changed
src/Datastructure/Lists/ArrayLists Expand file tree Collapse file tree 2 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -223,6 +223,20 @@ public function remove($key): bool {
223
223
return false ;
224
224
}
225
225
226
+ /**
227
+ * whether the array contains $key or not.
228
+ *
229
+ * @param int $key
230
+ * @return bool
231
+ */
232
+ public function containsKey (int $ key ): bool {
233
+ $ array = $ this ->array ;
234
+ $ array = \array_filter ($ array , function ($ value , $ key ) {
235
+ return $ value !== null ;
236
+ }, \ARRAY_FILTER_USE_BOTH );
237
+ return array_key_exists ($ key , $ array );
238
+ }
239
+
226
240
/**
227
241
* removes all elements in the array that are null or equal to empty string
228
242
*
Original file line number Diff line number Diff line change 1
1
<?php
2
-
3
- use doganoo \PHPAlgorithms \Datastructure \Lists \ArrayLists \ArrayList ;
4
-
5
2
/**
6
3
* MIT License
7
4
*
25
22
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
23
* SOFTWARE.
27
24
*/
25
+
26
+ use doganoo \PHPAlgorithms \Datastructure \Lists \ArrayLists \ArrayList ;
27
+
28
28
class ArrayListTest extends \PHPUnit \Framework \TestCase {
29
29
public function testAdd () {
30
30
$ arrayList = new ArrayList ();
@@ -60,6 +60,23 @@ public function testAdd() {
60
60
$ this ->assertTrue ($ arrayList ->length () === 8 );
61
61
}
62
62
63
+ public function testContainsKey () {
64
+ $ arrayList = new ArrayList ();
65
+ $ arrayList ->add ("five " );
66
+ $ arrayList ->add ("six " );
67
+ $ arrayList ->add ("seven " );
68
+ $ arrayList ->add ("eight " );
69
+
70
+ $ this ->assertTrue (true === $ arrayList ->containsKey (0 ));
71
+ $ this ->assertTrue (true === $ arrayList ->containsKey (1 ));
72
+ $ this ->assertTrue (true === $ arrayList ->containsKey (2 ));
73
+ $ this ->assertTrue (true === $ arrayList ->containsKey (3 ));
74
+ $ this ->assertTrue (false === $ arrayList ->containsKey (4 ));
75
+ $ this ->assertTrue (false === $ arrayList ->containsKey (50 ));
76
+ $ this ->assertTrue (false === $ arrayList ->containsKey (150 ));
77
+
78
+ }
79
+
63
80
public function testClear () {
64
81
$ arrayList = new ArrayList ();
65
82
$ arrayList ->add ("one " );
You can’t perform that action at this time.
0 commit comments