diff --git a/core-java-modules/core-java-exceptions-4/pom.xml b/core-java-modules/core-java-exceptions-4/pom.xml new file mode 100644 index 0000000000..cc81fdc40b --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + com.baeldung.exceptions + core-java-exceptions-4 + 0.1.0-SNAPSHOT + core-java-exceptions-4 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + com.h2database + h2 + ${h2.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + + + + + + + + + + 1.4.191 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java new file mode 100644 index 0000000000..6b320976a6 --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java @@ -0,0 +1,31 @@ +package com.baeldung.exception.arrayindexoutofbounds; + +import java.util.Arrays; +import java.util.List; + +public class ArrayIndexOutOfBoundsExceptionDemo { + + public static void main(String[] args) { + int[] numbers = new int[] { 1, 2, 3, 4, 5 }; + + getArrayElementAtIndex(numbers, 5); + getListElementAtIndex(5); + addArrayElementsUsingLoop(numbers); + } + + public static void addArrayElementsUsingLoop(int[] numbers) { + int sum = 0; + for (int i = 0; i <= numbers.length; i++) { + sum += numbers[i]; + } + } + + public static int getListElementAtIndex(int index) { + List numbersList = Arrays.asList(1, 2, 3, 4, 5); + return numbersList.get(index); + } + + public static int getArrayElementAtIndex(int[] numbers, int index) { + return numbers[index]; + } +} diff --git a/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java new file mode 100644 index 0000000000..e1ad2b8021 --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.exception.arrayindexoutofbounds; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class ArrayIndexOutOfBoundsExceptionDemoUnitTest { + + private static int[] numbers; + + @BeforeAll + public static void setUp() { + numbers = new int[] { 1, 2, 3, 4, 5 }; + } + + @Test + void givenAnArrayOfSizeFive_whenAccessedElementBeyondRange_thenShouldThrowArrayIndexOutOfBoundsException() { + assertThrows(ArrayIndexOutOfBoundsException.class, + () -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers)); + } + + @Test + void givenAnArrayOfSizeFive_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() { + assertThrows(ArrayIndexOutOfBoundsException.class, + () -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5)); + } + + @Test + void givenAListReturnedByArraysAsListMethod_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() { + assertThrows(ArrayIndexOutOfBoundsException.class, + () -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5)); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 8404d75024..a25cf11454 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -56,6 +56,7 @@ core-java-exceptions core-java-exceptions-2 core-java-exceptions-3 + core-java-exceptions-4 core-java-function core-java-functional core-java-io