| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c9bda3d commit 2ae1bdf
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,7 +63,8 @@ public void add(final E element) { | |||
| 63 | 63 | * | |
| 64 | 64 | * @param index the index at which the element is to be placed | |
| 65 | 65 | * @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 | ||
| 67 | 68 | */ | |
| 68 | 69 | public void put(final int index, E element) { | |
| 69 | 70 | if (index < 0) { | |
@@ -82,7 +83,8 @@ public void put(final int index, E element) { | |||
| 82 | 83 | * | |
| 83 | 84 | * @param index the index of the element to retrieve | |
| 84 | 85 | * @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 | ||
| 86 | 88 | */ | |
| 87 | 89 | @SuppressWarnings("unchecked") | |
| 88 | 90 | public E get(final int index) { | |
@@ -97,7 +99,8 @@ public E get(final int index) { | |||
| 97 | 99 | * | |
| 98 | 100 | * @param index the index of the element to be removed | |
| 99 | 101 | * @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 | ||
| 101 | 104 | */ | |
| 102 | 105 | public E remove(final int index) { | |
| 103 | 106 | if (index < 0 || index >= size) { | |
@@ -127,6 +130,21 @@ public boolean isEmpty() { | |||
| 127 | 130 | return size == 0; | |
| 128 | 131 | } | |
| 129 | 132 | ||
| 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 | + | ||
| 130 | 148 | /** | |
| 131 | 149 | * Returns a sequential stream with this collection as its source. | |
| 132 | 150 | * | |
@@ -137,7 +155,8 @@ public Stream<E> stream() { | |||
| 137 | 155 | } | |
| 138 | 156 | ||
| 139 | 157 | /** | |
| 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. | ||
| 141 | 160 | * | |
| 142 | 161 | * @param minCapacity the minimum capacity required | |
| 143 | 162 | */ | |
@@ -150,7 +169,8 @@ private void ensureCapacity(int minCapacity) { | |||
| 150 | 169 | ||
| 151 | 170 | /** | |
| 152 | 171 | * 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. | ||
| 154 | 174 | * | |
| 155 | 175 | * @param index the index of the element to remove | |
| 156 | 176 | */ | |
@@ -163,7 +183,8 @@ private void fastRemove(int index) { | |||
| 163 | 183 | } | |
| 164 | 184 | ||
| 165 | 185 | /** | |
| 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. | ||
| 167 | 188 | * | |
| 168 | 189 | * @return a string containing the elements in the array | |
| 169 | 190 | */ | |
@@ -227,7 +248,9 @@ public E next() { | |||
| 227 | 248 | /** | |
| 228 | 249 | * Removes the last element returned by this iterator. | |
| 229 | 250 | * | |
| 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 | ||
| 231 | 254 | */ | |
| 232 | 255 | @Override | |
| 233 | 256 | public void remove() { | |
@@ -242,7 +265,8 @@ public void remove() { | |||
| 242 | 265 | /** | |
| 243 | 266 | * Checks for concurrent modifications to the array during iteration. | |
| 244 | 267 | * | |
| 245 | - * @throws ConcurrentModificationException if the array has been modified structurally | ||
| 268 | + * @throws ConcurrentModificationException if the array has been modified | ||
| 269 | + * structurally | ||
| 246 | 270 | */ | |
| 247 | 271 | private void checkForComodification() { | |
| 248 | 272 | if (modCount != expectedModCount) { | |
@@ -251,7 +275,8 @@ private void checkForComodification() { | |||
| 251 | 275 | } | |
| 252 | 276 | ||
| 253 | 277 | /** | |
| 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. | ||
| 255 | 280 | * | |
| 256 | 281 | * @param action the action to be performed for each element | |
| 257 | 282 | * @throws NullPointerException if the specified action is null | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,4 +255,23 @@ public void testCapacityDoubling() { | |||
| 255 | 255 | assertEquals(3, array.getSize()); | |
| 256 | 256 | assertEquals("Charlie", array.get(2)); | |
| 257 | 257 | } | |
| 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 | + } | ||
| 258 | 277 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments