Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,656 questions

55,403 answers

573 users

How to iterating over 2D array in Java

3 Answers

0 votes
public class Main {
    public static void main(String[] args) {
        int[][] array = {    
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };

        for (int i = 0; i < array.length; i++) { // Outer loop for rows
            for (int j = 0; j < array[i].length; j++) { // Inner loop for columns
                System.out.print(array[i][j] + " ");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}




/*
run:

1 2 3 
4 5 6 
7 8 9 

*/

 



answered Jul 22, 2025 by avibootz
0 votes
public class Main {
    public static void main(String[] args) {
        int[][] array = {    
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };

        for (int[] row : array) { // Outer loop for rows
            for (int element : row) { // Inner loop for elements in the row
                System.out.print(element + " ");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}




/*
run:

1 2 3 
4 5 6 
7 8 9 

*/

 



answered Jul 22, 2025 by avibootz
0 votes
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] array = {    
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };

        Arrays.stream(array).forEach(row -> {
            Arrays.stream(row).forEach(element -> System.out.print(element + " "));
            System.out.println(); // Move to the next line after each row
        });
    }
}




/*
run:

1 2 3 
4 5 6 
7 8 9 

*/

 



answered Jul 22, 2025 by avibootz
...