Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, November 11, 2021

Java Program - How to Print an Array in Java?

1. Introduction


In this tutorial, we'll learn different techniques on how to print the elements of a given array in Java.
                              Java Program - How to Print an Array in Java?

2. Example To Print Array of Numbers using for loop


In the below example for-each loop is used to iterate the array of integer numbers.

for each is also called as enhanced for loop.

[package com.javaprogramto.arrays.print;

public class PrintArray {
public static void main(String[] args) {

int[] array = { 1, 2, 3, 4, 5, 6, 7 };

for (int value : array) {
System.out.println(value);
}

}
}]



Output:

For each work well for arrays and collections api such as ArrayList and HashSet.

Within for loop, it accesses the each value in the array and prints the value using the println() method.

[1
2
3
4
5
6
7]

3. Example To Print Array's using Java API Arrays class


[package com.javaprogramto.arrays.print;

import java.util.Arrays;

public class PrintArrays {

public static void main(String[] args) {
int[] values = { 10, 20, 30, 40, 50 };

String value = Arrays.toString(values);
System.out.println(value);

}
}]

Java API comes with Arrays class that has a method toString() that takes an integer array as input. This will convert int[] array to String.

Output:

[[10, 20, 30, 40, 50]]

3. Example 3 To Print Multi Dimensional Array


As of now, you have seen printing the single dimension array and now will print the two or multidimensional array using Arrays.toString() method.

[package com.javaprogramto.arrays.print;

import java.util.Arrays;

public class PrintMultiDimenstionalArray {

public static void main(String[] args) {

int[][] twoArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9, 0 } };

System.out.println("Using Arrays.toString : " + Arrays.toString(twoArray));

System.out.println("Using Arrays.deepToString : " + Arrays.deepToString(twoArray));

}
}]

Output:

[Using Arrays.toString : [[I@4e25154f, [I@70dea4e, [I@5c647e05]
Using Arrays.deepToString : [[1, 2, 3], [4, 5, 6], [7, 8, 9, 0]]]

First, as seen in the earlier example directly invoked Arrays.toString() method to print the array values but it printed the address of actual values in the nested array.

To get the actual values, you need to use Arrays.deepToString() method which takes Object[] so any type of array can be passed and it internally checks the type of the array and print the actual values from multi dimensional array.


4. Conclusion

In conclusion, You've seen how to print the array of values using for each, Arrays.toString(), and also print nested or multi dimensional arrays using Arrays.deepToString() which prints the actual values rather than nested array address.

As usual, All the examples are shown available over GitHub.

Share to see the GitHub Code.


[View on GitHub ##eye##]

[Download ##file-download##]




No comments:

Post a Comment

Please do not add any spam links in the comments section.