-
Notifications
You must be signed in to change notification settings - Fork 26
Enhancement #60: Added Nested Arrays Documentation #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,64 @@ Mr. Smith | |
Ms. Jones | ||
``` | ||
|
||
<a id="jagged"> </a> | ||
## Working with Jagged Arrays | ||
|
||
A **jagged array** (an array of arrays) in Java lets each row have a different length. For example, one row might have 3 elements while another has 2. To create a jagged array, first declare the outer array with a size but no specified inner lengths, then assign each sub-array its own size. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are rectangular arrays introduced anywhere? If you are explaining arrays having different sizes, it might also be worth covering that you can create arrays with given dimension sizes. |
||
|
||
--- | ||
|
||
## Step 1 – Declare the jagged array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm unsure about the use of headings here being a good idea. |
||
```java | ||
int[][] jaggedArray = new int[3][]; | ||
``` | ||
|
||
## Step 2 – Initialize each row with different sizes or values | ||
```java | ||
jaggedArray[0] = new int[] {1, 2, 3}; | ||
jaggedArray[1] = new int[] {4, 5}; | ||
jaggedArray[2] = new int[] {6, 7, 8, 9}; | ||
``` | ||
Each assignment gives a different length (3, 2, and 4 elements respectively) for rows 0, 1, and 2. | ||
|
||
## Step 3 – Use nested loops to print all elements | ||
|
||
```java | ||
System.out.println("Jagged array elements:"); | ||
for (int i = 0; i < jaggedArray.length; i++) { | ||
for (int j = 0; j < jaggedArray[i].length; j++) { | ||
System.out.print(jaggedArray[i][j] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
``` | ||
This will print every element in the jagged array in row-major order. | ||
|
||
## Step 4 – Print array lengths | ||
```java | ||
System.out.println("Outer array length: " + jaggedArray.length); | ||
for (int i = 0; i < jaggedArray.length; i++) { | ||
System.out.println("Length of row " + i + ": " + jaggedArray[i].length); | ||
} | ||
``` | ||
This outputs the size of the outer array and each inner row length, confirming the structure of the jagged array. | ||
|
||
### Output: | ||
```java | ||
Jagged array elements: | ||
1 2 3 | ||
4 5 | ||
6 7 8 9 | ||
Outer array length: 3 | ||
Length of row 0: 3 | ||
Length of row 1: 2 | ||
Length of row 2: 4 | ||
|
||
``` | ||
|
||
|
||
|
||
|
||
Finally, you can use the built-in `length` property to determine the size of any array. The following code prints the array's size to standard output: | ||
|
||
```java | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would need to be added to the table of contents (see the beginning of the Markdown file).
At that point, you might also want to change the last updated timestamp.