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 <vaibhav.ashokjain@vodafone.com>
This commit is contained in:
vaibhav007jain
2022-02-08 13:45:38 +05:30
committed by GitHub
parent da407fb601
commit b88ec4fdad
4 changed files with 113 additions and 0 deletions

View File

@@ -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<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5);
return numbersList.get(index);
}
public static int getArrayElementAtIndex(int[] numbers, int index) {
return numbers[index];
}
}

View File

@@ -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));
}
}