Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ public int getSize() {
public boolean isEmpty() {
return size == 0;
}
/**
* Checks whether the array contains the specified element.
*
* @param element the element to check for
* @return true if the array contains the specified element, false otherwise
*/
public boolean contains(final E element) {
for (int i = 0; i < size; i++) {
if (Objects.equals(elements[i], element)) {
return true;
}
}
return false;
}


/**
* Returns a sequential stream with this collection as its source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,23 @@ public void testCapacityDoubling() {
assertEquals(3, array.getSize());
assertEquals("Charlie", array.get(2));
}
@Test
public void testContains() {
DynamicArray<Integer> array = new DynamicArray<>();
array.add(1);
array.add(2);
array.add(3);

assertTrue(array.contains(2));
assertFalse(array.contains(5));
}

@Test
public void testContainsWithNull() {
DynamicArray<String> array = new DynamicArray<>();
array.add(null);

assertTrue(array.contains(null));
}

}
Loading