Skip to content

Commit dc60351

Browse files
authored
Merge pull request eugenp#4397 from shubhi22/BAEL-1761
BAEL-1761 Jagged Arrays In Java
2 parents 5836bfa + 642bff3 commit dc60351

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.array;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class JaggedArray {
7+
8+
int[][] shortHandFormInitialization() {
9+
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
10+
return jaggedArr;
11+
}
12+
13+
int[][] declarationAndThenInitialization() {
14+
int[][] jaggedArr = new int[3][];
15+
jaggedArr[0] = new int[] { 1, 2 };
16+
jaggedArr[1] = new int[] { 3, 4, 5 };
17+
jaggedArr[2] = new int[] { 6, 7, 8, 9 };
18+
return jaggedArr;
19+
}
20+
21+
int[][] declarationAndThenInitializationUsingUserInputs() {
22+
int[][] jaggedArr = new int[3][];
23+
jaggedArr[0] = new int[2];
24+
jaggedArr[1] = new int[3];
25+
jaggedArr[2] = new int[4];
26+
initializeElements(jaggedArr);
27+
return jaggedArr;
28+
}
29+
30+
void initializeElements(int[][] jaggedArr) {
31+
Scanner sc = new Scanner(System.in);
32+
for (int outer = 0; outer < jaggedArr.length; outer++) {
33+
for (int inner = 0; inner < jaggedArr[outer].length; inner++) {
34+
jaggedArr[outer][inner] = sc.nextInt();
35+
}
36+
}
37+
}
38+
39+
void printElements(int[][] jaggedArr) {
40+
for (int index = 0; index < jaggedArr.length; index++) {
41+
System.out.println(Arrays.toString(jaggedArr[index]));
42+
}
43+
}
44+
45+
int[] getElementAtGivenIndex(int[][] jaggedArr, int index) {
46+
return jaggedArr[index];
47+
}
48+
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.array;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import java.io.ByteArrayInputStream;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.InputStream;
9+
import java.io.PrintStream;
10+
11+
import org.junit.Test;
12+
13+
public class JaggedArrayTest {
14+
15+
private JaggedArray obj = new JaggedArray();
16+
17+
@Test
18+
public void whenInitializedUsingShortHandForm_thenCorrect() {
19+
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.shortHandFormInitialization());
20+
}
21+
22+
@Test
23+
public void whenInitializedWithDeclarationAndThenInitalization_thenCorrect() {
24+
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitialization());
25+
}
26+
27+
@Test
28+
public void whenInitializedWithDeclarationAndThenInitalizationUsingUserInputs_thenCorrect() {
29+
InputStream is = new ByteArrayInputStream("1 2 3 4 5 6 7 8 9".getBytes());
30+
System.setIn(is);
31+
assertArrayEquals(new int[][] { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }, obj.declarationAndThenInitializationUsingUserInputs());
32+
System.setIn(System.in);
33+
}
34+
35+
@Test
36+
public void givenJaggedArrayAndAnIndex_thenReturnArrayAtGivenIndex() {
37+
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
38+
assertArrayEquals(new int[] { 1, 2 }, obj.getElementAtGivenIndex(jaggedArr, 0));
39+
assertArrayEquals(new int[] { 3, 4, 5 }, obj.getElementAtGivenIndex(jaggedArr, 1));
40+
assertArrayEquals(new int[] { 6, 7, 8, 9 }, obj.getElementAtGivenIndex(jaggedArr, 2));
41+
}
42+
43+
@Test
44+
public void givenJaggedArray_whenUsingArraysAPI_thenVerifyPrintedElements() {
45+
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
46+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
47+
System.setOut(new PrintStream(outContent));
48+
obj.printElements(jaggedArr);
49+
assertEquals("[1, 2]\n[3, 4, 5]\n[6, 7, 8, 9]\n", outContent.toString());
50+
System.setOut(System.out);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)