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:
parent
d0e9a520fe
commit
1bf44ff8ef
|
@ -0,0 +1,47 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.exceptions</groupId>
|
||||
<artifactId>core-java-exceptions-4</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-exceptions-4</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<compilerArgs>
|
||||
<!-- Comment the arg below to print complete stack trace information -->
|
||||
<!-- <arg>-g:none</arg>-->
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<h2.version>1.4.191</h2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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];
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -56,6 +56,7 @@
|
|||
<module>core-java-exceptions</module>
|
||||
<module>core-java-exceptions-2</module>
|
||||
<module>core-java-exceptions-3</module>
|
||||
<module>core-java-exceptions-4</module>
|
||||
<module>core-java-function</module>
|
||||
<module>core-java-functional</module>
|
||||
<module>core-java-io</module>
|
||||
|
|
Loading…
Reference in New Issue