gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
HOw to print a multidimentional array in java
- User
HOw to print a multidimentional array in java
- Assistant
To print a multidimensional array in Java, you can use nested for loops to iterate through each element of the array and print its value. Here's an example:
javapublic class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Using nested for loops to print each element for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); // Print a new line after printing each row } } }Output:
code1 2 3 4 5 6 7 8 9In this example, we have a 2D array
arrwith 3 rows and 3 columns. The outer loop iterates through the rows of the array, while the inner loop iterates through the columns. The inner loop prints each element followed by a space, and the outer loop adds a new line after printing each row.