Skip to content

Commit 2ae1bdf

Browse files
feat: add contains() method to DynamicArray (#7270)
* feat: add contains() method to DynamicArray * style: fix clang formatting * style: fix clang format issues * style: apply remaining clang formatting --------- Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
1 parent c9bda3d commit 2ae1bdf

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void add(final E element) {
6363
*
6464
* @param index the index at which the element is to be placed
6565
* @param element the element to be inserted at the specified index
66-
* @throws IndexOutOfBoundsException if index is less than 0 or greater than or equal to the number of elements
66+
* @throws IndexOutOfBoundsException if index is less than 0 or greater than or
67+
* equal to the number of elements
6768
*/
6869
public void put(final int index, E element) {
6970
if (index < 0) {
@@ -82,7 +83,8 @@ public void put(final int index, E element) {
8283
*
8384
* @param index the index of the element to retrieve
8485
* @return the element at the specified index
85-
* @throws IndexOutOfBoundsException if index is less than 0 or greater than or equal to the current size
86+
* @throws IndexOutOfBoundsException if index is less than 0 or greater than or
87+
* equal to the current size
8688
*/
8789
@SuppressWarnings("unchecked")
8890
public E get(final int index) {
@@ -97,7 +99,8 @@ public E get(final int index) {
9799
*
98100
* @param index the index of the element to be removed
99101
* @return the element that was removed from the array
100-
* @throws IndexOutOfBoundsException if index is less than 0 or greater than or equal to the current size
102+
* @throws IndexOutOfBoundsException if index is less than 0 or greater than or
103+
* equal to the current size
101104
*/
102105
public E remove(final int index) {
103106
if (index < 0 || index >= size) {
@@ -127,6 +130,21 @@ public boolean isEmpty() {
127130
return size == 0;
128131
}
129132

133+
/**
134+
* Checks whether the array contains the specified element.
135+
*
136+
* @param element the element to check for
137+
* @return true if the array contains the specified element, false otherwise
138+
*/
139+
public boolean contains(final E element) {
140+
for (int i = 0; i < size; i++) {
141+
if (Objects.equals(elements[i], element)) {
142+
return true;
143+
}
144+
}
145+
return false;
146+
}
147+
130148
/**
131149
* Returns a sequential stream with this collection as its source.
132150
*
@@ -137,7 +155,8 @@ public Stream<E> stream() {
137155
}
138156

139157
/**
140-
* Ensures that the array has enough capacity to hold the specified number of elements.
158+
* Ensures that the array has enough capacity to hold the specified number of
159+
* elements.
141160
*
142161
* @param minCapacity the minimum capacity required
143162
*/
@@ -150,7 +169,8 @@ private void ensureCapacity(int minCapacity) {
150169

151170
/**
152171
* Removes the element at the specified index without resizing the array.
153-
* This method shifts any subsequent elements to the left and clears the last element.
172+
* This method shifts any subsequent elements to the left and clears the last
173+
* element.
154174
*
155175
* @param index the index of the element to remove
156176
*/
@@ -163,7 +183,8 @@ private void fastRemove(int index) {
163183
}
164184

165185
/**
166-
* Returns a string representation of the array, including only the elements that are currently stored.
186+
* Returns a string representation of the array, including only the elements
187+
* that are currently stored.
167188
*
168189
* @return a string containing the elements in the array
169190
*/
@@ -227,7 +248,9 @@ public E next() {
227248
/**
228249
* Removes the last element returned by this iterator.
229250
*
230-
* @throws IllegalStateException if the next method has not yet been called, or the remove method has already been called after the last call to the next method
251+
* @throws IllegalStateException if the next method has not yet been called, or
252+
* the remove method has already been called after
253+
* the last call to the next method
231254
*/
232255
@Override
233256
public void remove() {
@@ -242,7 +265,8 @@ public void remove() {
242265
/**
243266
* Checks for concurrent modifications to the array during iteration.
244267
*
245-
* @throws ConcurrentModificationException if the array has been modified structurally
268+
* @throws ConcurrentModificationException if the array has been modified
269+
* structurally
246270
*/
247271
private void checkForComodification() {
248272
if (modCount != expectedModCount) {
@@ -251,7 +275,8 @@ private void checkForComodification() {
251275
}
252276

253277
/**
254-
* Performs the given action for each remaining element in the iterator until all elements have been processed.
278+
* Performs the given action for each remaining element in the iterator until
279+
* all elements have been processed.
255280
*
256281
* @param action the action to be performed for each element
257282
* @throws NullPointerException if the specified action is null

src/test/java/com/thealgorithms/datastructures/dynamicarray/DynamicArrayTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,23 @@ public void testCapacityDoubling() {
255255
assertEquals(3, array.getSize());
256256
assertEquals("Charlie", array.get(2));
257257
}
258+
259+
@Test
260+
public void testContains() {
261+
DynamicArray<Integer> array = new DynamicArray<>();
262+
array.add(1);
263+
array.add(2);
264+
array.add(3);
265+
266+
assertTrue(array.contains(2));
267+
assertFalse(array.contains(5));
268+
}
269+
270+
@Test
271+
public void testContainsWithNull() {
272+
DynamicArray<String> array = new DynamicArray<>();
273+
array.add(null);
274+
275+
assertTrue(array.contains(null));
276+
}
258277
}

0 commit comments

Comments
 (0)