From 1bf44ff8ef31887bc85fd77f09ad7b69b0f64216 Mon Sep 17 00:00:00 2001 From: vaibhav007jain <72961247+vaibhav007jain@users.noreply.github.com> Date: Tue, 8 Feb 2022 13:45:38 +0530 Subject: [PATCH] BAEL-5280: added code for ArrayIndexOutOfBoundsException. (#11772) * commited initial code for hexagonal architecture * Deleting to check in again * Deleing to check in again * Push first code for Hexagonal Architecture * final code with UT for JSON to Java conversion * removed hexagonal-architecture code from last commit * BEL-5071 updated README * BAEL-5071: Undo README changes and added a nested object in the JSON example. * BAEL-5071: fixed whitespace/indentation in JsonToJavaClassConversion.java * BAEL-5151: Added code for getting the first of a String * BAEL-5151: Renamed Unit Test * BAEL-5151: Moved the files from core-java-string-operations to core-java-string-operations-3 * BAEL-5151: Replaced tabs with white spaces. * BAEL-5228: Adding code for approaches to concatening null string in java * BAEL-5228: Moved file to correct folder and added a UT. * BAEL-5228: corrected import statements. * BAEL-5228: corrected last commit. * BAEL-5228: removed extra import. * BAEL-5228: renamed UT * BAEL-5280: added code for ArrayIndexOutOfBoundsException. * BAEL-5280: moved code for ArrayIndexOutOfBoundsException in a new module. * BAEL-5280: Fixed tab/spaces in pom.xml. * BAEL-5280: Fixed indentation in ArrayIndexOutOfBoundsExceptionDemoUnitTest.java. Co-authored-by: Vaibhav Jain --- .../core-java-exceptions-4/pom.xml | 47 +++++++++++++++++++ .../ArrayIndexOutOfBoundsExceptionDemo.java | 31 ++++++++++++ ...IndexOutOfBoundsExceptionDemoUnitTest.java | 34 ++++++++++++++ core-java-modules/pom.xml | 1 + 4 files changed, 113 insertions(+) create mode 100644 core-java-modules/core-java-exceptions-4/pom.xml create mode 100644 core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java create mode 100644 core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java 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