Jagged Arrays & Table Formatting
Handle jagged 2D arrays and print aligned tables using per-column widths; demo builds a sample matrix without Scanner.
Jagged Arrays & Table Formatting is a free Java Academy lesson on CoddyKit — lesson 2 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Jagged Concept
Jagged 2D arrays: each row can have a different length. Treat rows separately, never assume a common column count.
Create Jagged
Create a jagged array by allocating rows with different lengths. The message explains why: we only need as many columns as each row requires.
public class Main {
public static void main(String[] args) {
// Create a jagged array with 3 rows of varying lengths
int[][] m = new int[3][];
// Row 0 has 2 columns
m[0] = new int[]{1, 2};
// Row 1 has 3 columns
m[1] = new int[]{3, 4, 5};
// Row 2 has 1 column
m[2] = new int[]{6};
// Print the jagged array
System.out.println("Jagged array contents:");
for (int r = 0; r < m.length; r++) {
for (int c = 0; c < m[r].length; c++) {
System.out.print(m[r][c] + " ");
}
System.out.println(); // new line after each row
}
}
}
Safe Iterate
Iterate safely by using m[r].length for each row. The inner loop length may differ per row.
public class Main {
public static void main(String[] args) {
// Example jagged array
int[][] m = {
{1, 2},
{3, 4, 5},
{6}
};
// Safe nested loops for a jagged matrix
for (int r = 0; r < m.length; r = r + 1) {
// Always use the actual length of the current row
for (int c = 0; c < m[r].length; c = c + 1) {
int value = m[r][c]; // process the value
System.out.print(value + " ");
}
System.out.println(); // new line after each row
}
}
}
Column Widths
To print aligned columns, compute a width for each existing column by scanning all rows.
// Determine the maximum number of columns among all rows
int maxCols = 0;
for (int r = 0; r < m.length; r = r + 1) {
if (m[r].length > maxCols) maxCols = m[r].length;
}
// For each column, store the maximum string length of values in that column
int[] w = new int[maxCols];
for (int r = 0; r < m.length; r = r + 1) {
for (int c = 0; c < m[r].length; c = c + 1) {
int len = Integer.toString(m[r][c]).length();
if (len > w[c]) w[c] = len;
}
}Aligned Print
Pad each value on the left until it reaches the column width, then print the row as a single string.
// Print each row with left-padded columns
for (int r = 0; r < m.length; r = r + 1) {
String line = "";
for (int c = 0; c < m[r].length; c = c + 1) {
String s = Integer.toString(m[r][c]);
while (s.length() < w[c]) s = " " + s; // left pad with spaces
line = line + s + (c + 1 < m[r].length ? " " : "");
}
System.out.println(line);
}No-Input Demo
Run it: This demo builds a jagged matrix in code, computes column widths, and prints an aligned table—no input required.
public class Main {
// Compute per-column widths for a jagged matrix
static int[] colWidths(int[][] m) {
int maxCols = 0;
for (int r = 0; r < m.length; r = r + 1) {
if (m[r].length > maxCols) maxCols = m[r].length;
}
int[] w = new int[maxCols];
for (int r = 0; r < m.length; r = r + 1) {
for (int c = 0; c < m[r].length; c = c + 1) {
int len = Integer.toString(m[r][c]).length();
if (len > w[c]) w[c] = len;
}
}
// Ensure at least width 1 for each column
for (int c = 0; c < w.length; c = c + 1) {
if (w[c] < 1) w[c] = 1;
}
return w;
}
// Print the matrix with aligned columns
static void printTable(int[][] m) {
int[] w = colWidths(m);
for (int r = 0; r < m.length; r = r + 1) {
String line = "";
for (int c = 0; c < m[r].length; c = c + 1) {
String s = Integer.toString(m[r][c]);
while (s.length() < w[c]) s = " " + s;
line = line + s + (c + 1 < m[r].length ? " " : "");
}
System.out.println(line);
}
}
public static void main(String[] args) {
// Sample jagged matrix (no Scanner; values are hard-coded)
int[][] m = {
{1, 23, 456},
{7, 8},
{90, 12, 3, 4}
};
System.out.println("Aligned table:");
printTable(m);
}
}
Jagged Iterate Check
Quick check: Which nested loop safely iterates every element in a jagged matrix?
Recap & Next
Recap: You created jagged arrays, iterated safely with per-row lengths, computed column widths, and printed aligned tables—without Scanner.
Next: Explore row/column maxima and the transpose of a matrix.
Frequently asked questions
Is the “Jagged Arrays & Table Formatting” lesson free?
Yes — the full text of “Jagged Arrays & Table Formatting” is free to read here on the web, and the Java Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Jagged Arrays & Table Formatting”?
Handle jagged 2D arrays and print aligned tables using per-column widths; demo builds a sample matrix without Scanner. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 6, so you can start here or from the beginning and move at your own pace.
How long does the “Jagged Arrays & Table Formatting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.