[BAEL-2999] Improve Jagged Arrays article (#7201)
* Hexagonal Architecture in Java * Hexagonal Architecture in Java - incorporated feedback * BAEL-2999 * Removing the evaluation article code. * Corrected indentation for continuations
This commit is contained in:
parent
c4ccb08b55
commit
02cb552470
|
@ -1,49 +0,0 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class JaggedArray {
|
||||
|
||||
int[][] shortHandFormInitialization() {
|
||||
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
return jaggedArr;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitialization() {
|
||||
int[][] jaggedArr = new int[3][];
|
||||
jaggedArr[0] = new int[] { 1, 2 };
|
||||
jaggedArr[1] = new int[] { 3, 4, 5 };
|
||||
jaggedArr[2] = new int[] { 6, 7, 8, 9 };
|
||||
return jaggedArr;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitializationUsingUserInputs() {
|
||||
int[][] jaggedArr = new int[3][];
|
||||
jaggedArr[0] = new int[2];
|
||||
jaggedArr[1] = new int[3];
|
||||
jaggedArr[2] = new int[4];
|
||||
initializeElements(jaggedArr);
|
||||
return jaggedArr;
|
||||
}
|
||||
|
||||
void initializeElements(int[][] jaggedArr) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
for (int outer = 0; outer < jaggedArr.length; outer++) {
|
||||
for (int inner = 0; inner < jaggedArr[outer].length; inner++) {
|
||||
jaggedArr[outer][inner] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printElements(int[][] jaggedArr) {
|
||||
for (int index = 0; index < jaggedArr.length; index++) {
|
||||
System.out.println(Arrays.toString(jaggedArr[index]));
|
||||
}
|
||||
}
|
||||
|
||||
int[] getElementAtGivenIndex(int[][] jaggedArr, int index) {
|
||||
return jaggedArr[index];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MultiDimensionalArray {
|
||||
|
||||
int[][] shortHandFormInitialization() {
|
||||
int[][] multiDimensionalArray = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
return multiDimensionalArray;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitialization() {
|
||||
int[][] multiDimensionalArray = new int[3][];
|
||||
multiDimensionalArray[0] = new int[] { 1, 2 };
|
||||
multiDimensionalArray[1] = new int[] { 3, 4, 5 };
|
||||
multiDimensionalArray[2] = new int[] { 6, 7, 8, 9 };
|
||||
return multiDimensionalArray;
|
||||
}
|
||||
|
||||
int[][] declarationAndThenInitializationUsingUserInputs() {
|
||||
int[][] multiDimensionalArray = new int[3][];
|
||||
multiDimensionalArray[0] = new int[2];
|
||||
multiDimensionalArray[1] = new int[3];
|
||||
multiDimensionalArray[2] = new int[4];
|
||||
initializeElements(multiDimensionalArray);
|
||||
return multiDimensionalArray;
|
||||
}
|
||||
|
||||
void initializeElements(int[][] multiDimensionalArray) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
for (int outer = 0; outer < multiDimensionalArray.length; outer++) {
|
||||
for (int inner = 0; inner < multiDimensionalArray[outer].length; inner++) {
|
||||
multiDimensionalArray[outer][inner] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initialize2DArray(int[][] multiDimensionalArray) {
|
||||
for (int[] array : multiDimensionalArray) {
|
||||
Arrays.fill(array, 7);
|
||||
}
|
||||
}
|
||||
|
||||
void printElements(int[][] multiDimensionalArray) {
|
||||
for (int index = 0; index < multiDimensionalArray.length; index++) {
|
||||
System.out.println(Arrays.toString(multiDimensionalArray[index]));
|
||||
}
|
||||
}
|
||||
|
||||
int[] getElementAtGivenIndex(int[][] multiDimensionalArray, int index) {
|
||||
return multiDimensionalArray[index];
|
||||
}
|
||||
|
||||
int[] findLengthOfElements(int[][] multiDimensionalArray) {
|
||||
int[] arrayOfLengths = new int[multiDimensionalArray.length];
|
||||
for (int i = 0; i < multiDimensionalArray.length; i++) {
|
||||
arrayOfLengths[i] = multiDimensionalArray[i].length;
|
||||
}
|
||||
return arrayOfLengths;
|
||||
}
|
||||
|
||||
Integer[] findLengthOfElements(Integer[][] multiDimensionalArray) {
|
||||
return Arrays.stream(multiDimensionalArray)
|
||||
.map(array -> array.length)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
int[][] copy2DArray(int[][] arrayOfArrays) {
|
||||
int[][] copied2DArray = new int[arrayOfArrays.length][];
|
||||
for (int i = 0; i < arrayOfArrays.length; i++) {
|
||||
int[] array = arrayOfArrays[i];
|
||||
copied2DArray[i] = Arrays.copyOf(array, array.length);
|
||||
}
|
||||
return copied2DArray;
|
||||
}
|
||||
|
||||
Integer[][] copy2DArray(Integer[][] arrayOfArrays) {
|
||||
return Arrays.stream(arrayOfArrays)
|
||||
.map(array -> Arrays.copyOf(array, array.length))
|
||||
.toArray(Integer[][]::new);
|
||||
}
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JaggedArrayUnitTest {
|
||||
|
||||
private JaggedArray obj = new JaggedArray();
|
||||
|
||||
@Test
|
||||
public void whenInitializedUsingShortHandForm_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.shortHandFormInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalization_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalizationUsingUserInputs_thenCorrect() {
|
||||
InputStream is = new ByteArrayInputStream("1 2 3 4 5 6 7 8 9".getBytes());
|
||||
System.setIn(is);
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitializationUsingUserInputs());
|
||||
System.setIn(System.in);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJaggedArrayAndAnIndex_thenReturnArrayAtGivenIndex() {
|
||||
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new int[] { 1, 2 }, obj.getElementAtGivenIndex(jaggedArr, 0));
|
||||
assertArrayEquals(new int[] { 3, 4, 5 }, obj.getElementAtGivenIndex(jaggedArr, 1));
|
||||
assertArrayEquals(new int[] { 6, 7, 8, 9 }, obj.getElementAtGivenIndex(jaggedArr, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJaggedArray_whenUsingArraysAPI_thenVerifyPrintedElements() {
|
||||
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outContent));
|
||||
obj.printElements(jaggedArr);
|
||||
assertEquals("[1, 2][3, 4, 5][6, 7, 8, 9]", outContent.toString().replace("\r", "").replace("\n", ""));
|
||||
System.setOut(System.out);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package com.baeldung.array;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MultiDimensionalArrayUnitTest {
|
||||
|
||||
private MultiDimensionalArray obj = new MultiDimensionalArray();
|
||||
|
||||
@Test
|
||||
public void whenInitializedUsingShortHandForm_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.shortHandFormInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalization_thenCorrect() {
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitialization());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializedWithDeclarationAndThenInitalizationUsingUserInputs_thenCorrect() {
|
||||
InputStream is = new ByteArrayInputStream("1 2 3 4 5 6 7 8 9".getBytes());
|
||||
System.setIn(is);
|
||||
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitializationUsingUserInputs());
|
||||
System.setIn(System.in);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArrayAndAnIndex_thenReturnArrayAtGivenIndex() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new int[] { 1, 2 }, obj.getElementAtGivenIndex(multiDimensionalArr, 0));
|
||||
assertArrayEquals(new int[] { 3, 4, 5 }, obj.getElementAtGivenIndex(multiDimensionalArr, 1));
|
||||
assertArrayEquals(new int[] { 6, 7, 8, 9 }, obj.getElementAtGivenIndex(multiDimensionalArr, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysAPI_thenVerifyPrintedElements() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outContent));
|
||||
obj.printElements(multiDimensionalArr);
|
||||
assertEquals("[1, 2][3, 4, 5][6, 7, 8, 9]", outContent.toString().replace("\r", "").replace("\n", ""));
|
||||
System.setOut(System.out);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysFill_thenVerifyInitialize2DArray() {
|
||||
int[][] multiDimensionalArr = new int[3][];
|
||||
multiDimensionalArr[0] = new int[2];
|
||||
multiDimensionalArr[1] = new int[3];
|
||||
multiDimensionalArr[2] = new int[4];
|
||||
obj.initialize2DArray(multiDimensionalArr);
|
||||
assertArrayEquals(new int[][] {{7,7}, {7,7,7}, {7,7,7,7}}, multiDimensionalArr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingIteration_thenVerifyFindLengthOfElements() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new int[]{2,3,4}, obj.findLengthOfElements(multiDimensionalArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysStream_thenVerifyFindLengthOfElements() {
|
||||
Integer[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(new Integer[]{2,3,4}, obj.findLengthOfElements(multiDimensionalArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysCopyOf_thenVerifyCopy2DArray() {
|
||||
int[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(multiDimensionalArr, obj.copy2DArray(multiDimensionalArr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiDimensionalArray_whenUsingArraysStream_thenVerifyCopy2DArray() {
|
||||
Integer[][] multiDimensionalArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
|
||||
assertArrayEquals(multiDimensionalArr, obj.copy2DArray(multiDimensionalArr));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue