Sunday, January 19, 2025
HomeProgrammingWhat's The Simplest Way to Print a Java Array?

What’s The Simplest Way to Print a Java Array?

The simplest way to print a Java array is by using Arrays.toString() for a one-dimensional array or Arrays.deepToString() for a multi-dimensional array. First, import java.util.Arrays. Then, pass the array as an argument to these methods. Here’s an example:

java
import java.util.Arrays;

public class ArrayPrinter {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4};
System.out.println(Arrays.toString(numbers));

int[][] multiArray = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(multiArray));
}
}

These methods handle formatting for you, making array printing quick and straightforward.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x