Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we will see how to loop diagonally through a two-dimensional array. The solution that we provide can be used for a square two-dimensional array of any size.

2. Two-Dimensional Array

The key in working with elements of an array is knowing how to get a specific element from that array. For a two-dimensional array, we use row and column indices to get elements of an array. For this problem, we’ll use the following diagram to show how to get these elements.

LoopingDiagonallyThrough2DArray 2

Next, we need to understand how many diagonal lines we have in our array, as seen in the diagram. We do this by first getting the length of one dimension of the array and then using that to get the number of diagonal lines (diagonalLines).

We then use the number of diagonal lines to get the mid-point which will help in the search for row and column indices.

In this example, the mid-point is three:

int length = twoDArray.length
int diagonalLines = (length + length) - 1
int midPoint = (diagonalLines / 2) + 1

3. Getting Row and Column Indices

To loop through the whole array, we start looping from 1 until the loop variable is less than or equal to the diagonalLines variable.

for (int i = 1; i <= diagonalLines; i++) {
    // some operations
}

Let’s also introduce the idea of the number of items in a diagonal line, calling it itemsInDiagonal. For example, line 3 in the diagram above has 3 items (g, e, c) and line 4 has 2 (h, f). This variable is incremented by 1 in the loop when loop variable i is less or equal to midPoint. It is then decremented by 1 otherwise.

After incrementing or decrementing itemsInDiagonal, we then have a new loop with loop variable j. Variable j is incremented from 0 until it is less than itemsInDiagonal.

We then use loop variables i and j to get the row and column indices. The logic of this calculation depends on whether loop variable i is greater than midPoint or not. When i is greater than midPoint, we also use the length variable to determine the row and column indices:

int rowIndex;
int columnIndex;

if (i <= midPoint) {
    itemsInDiagonal++;
    for (int j = 0; j < itemsInDiagonal; j++) {
        rowIndex = (i - j) - 1;
        columnIndex = j;
        items.append(twoDArray[rowIndex][columnIndex]);
    }
} else {
    itemsInDiagonal--;
    for (int j = 0; j < itemsInDiagonal; j++) {
        rowIndex = (length - 1) - j;
        columnIndex = (i - length) + j;
        items.append(twoDArray[rowIndex][columnIndex]);
    }
}

4. Conclusion

In this tutorial, we have shown how to loop diagonally through a square two-dimensional array using a method that helps in getting row and column indices.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW