Skip to content

Commit ad88ef8

Browse files
DenizAltunkapanNeha-2005-VCE
authored andcommitted
Merge branch 'master' into feat-add-contains-dynamicarray
2 parents 49ebac9 + c8d0291 commit ad88ef8

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>6.0.2</version>
23+
<version>6.0.3</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>

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

Lines changed: 31 additions & 20 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,13 +99,15 @@ 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) {
104107
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
105108
}
106-
@SuppressWarnings("unchecked") E oldElement = (E) elements[index];
109+
@SuppressWarnings("unchecked")
110+
E oldElement = (E) elements[index];
107111
fastRemove(index);
108112
modCount++; // Increment modification count
109113
return oldElement;
@@ -126,21 +130,21 @@ public int getSize() {
126130
public boolean isEmpty() {
127131
return size == 0;
128132
}
133+
129134
/**
130-
* Checks whether the array contains the specified element.
131-
*
132-
* @param element the element to check for
133-
* @return true if the array contains the specified element, false otherwise
134-
*/
135+
* Checks whether the array contains the specified element.
136+
*
137+
* @param element the element to check for
138+
* @return true if the array contains the specified element, false otherwise
139+
*/
135140
public boolean contains(final E element) {
136141
for (int i = 0; i < size; i++) {
137-
if (Objects.equals(elements[i], element)) {
138-
return true;
142+
if (Objects.equals(elements[i], element)) {
143+
return true;
144+
}
139145
}
146+
return false;
140147
}
141-
return false;
142-
}
143-
144148

145149
/**
146150
* Returns a sequential stream with this collection as its source.
@@ -152,7 +156,8 @@ public Stream<E> stream() {
152156
}
153157

154158
/**
155-
* Ensures that the array has enough capacity to hold the specified number of elements.
159+
* Ensures that the array has enough capacity to hold the specified number of
160+
* elements.
156161
*
157162
* @param minCapacity the minimum capacity required
158163
*/
@@ -165,7 +170,8 @@ private void ensureCapacity(int minCapacity) {
165170

166171
/**
167172
* Removes the element at the specified index without resizing the array.
168-
* This method shifts any subsequent elements to the left and clears the last element.
173+
* This method shifts any subsequent elements to the left and clears the last
174+
* element.
169175
*
170176
* @param index the index of the element to remove
171177
*/
@@ -178,7 +184,8 @@ private void fastRemove(int index) {
178184
}
179185

180186
/**
181-
* Returns a string representation of the array, including only the elements that are currently stored.
187+
* Returns a string representation of the array, including only the elements
188+
* that are currently stored.
182189
*
183190
* @return a string containing the elements in the array
184191
*/
@@ -242,7 +249,9 @@ public E next() {
242249
/**
243250
* Removes the last element returned by this iterator.
244251
*
245-
* @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
252+
* @throws IllegalStateException if the next method has not yet been called, or
253+
* the remove method has already been called after
254+
* the last call to the next method
246255
*/
247256
@Override
248257
public void remove() {
@@ -257,7 +266,8 @@ public void remove() {
257266
/**
258267
* Checks for concurrent modifications to the array during iteration.
259268
*
260-
* @throws ConcurrentModificationException if the array has been modified structurally
269+
* @throws ConcurrentModificationException if the array has been modified
270+
* structurally
261271
*/
262272
private void checkForComodification() {
263273
if (modCount != expectedModCount) {
@@ -266,7 +276,8 @@ private void checkForComodification() {
266276
}
267277

268278
/**
269-
* Performs the given action for each remaining element in the iterator until all elements have been processed.
279+
* Performs the given action for each remaining element in the iterator until
280+
* all elements have been processed.
270281
*
271282
* @param action the action to be performed for each element
272283
* @throws NullPointerException if the specified action is null

0 commit comments

Comments
 (0)