1. Introduction
Have you ever wanted to organize your data but don’t know where to start? Look no further! Java arrays offer the perfect solution. With a single instance, they can easily store and access an array of objects by referencing its commanded index, which starts at 0 – talk about making things organized in style! This article will show multiple ways to print a Java Array.
2. Arrays to String
private static void printStringArray() {
String[] array = new String[]{"One", "Two", "Three"};
System.out.println(Arrays.toString(array));
// prints: [One, Two, Three]
}
Arrays.toString method prints the output of the array as a String. In the background, it uses StringBuilder to append each element into a String. Printing a numeric type array can also be achieved in the same way.
3. Java 8 For Each
private static void convertToListAndJava8ForEach() {
String[] array = new String[]{"One", "Two", "Three"};
Arrays.asList(array).forEach(System.out::println);
// One Two Three
}
Java 8 approach to print array elements requires shuffling all the elements to the list. Hence we can use Java 8 forEach to print each element in the new line because the list interface extends collection, and collection extends Iterable.
4. Java 8 Streams
private static void convertJava8StreamsAndForEach() {
String[] array = new String[]{"One", "Two", "Three"};
Arrays.stream(array).forEach(System.out::println);
// One Two Three
}
A different Java 8 solution is to use stream rather than converting to list. ForEach again will print each element using System.out via elegant method reference. Streams are helpful when you need to filter some elements conditionally.
5. Multi-Dimensional Array
private static void printMultiDimensionalArray() {
String[][] multiDimensionalArray =
new String[][]{{"One", "Two"}, {"Three", "Four"}};
System.out.println(Arrays.deepToString(multiDimensionalArray));
// [[One, Two], [Three, Four]]
}
Display of elements in a multi-dimensional array achieved using the deepToString method. Internally it also appends to a String using StringBuilder.
private static void printMultiDimensionalArrayJava8() {
String[][] multiDimensionalArray =
new String[][]{{"One", "Two"}, {"Three", "Four"}};
Arrays.stream(multiDimensionalArray)
.flatMap(Arrays::stream)
.forEach(System.out::println);
// One Two Three Four
}
More modern but bit bulky Java 8 approach via stream and flatMap to flatten array streams. The elements are displayed from each array according to the index assigned during the insertion. Perhaps much more practical if you need to filter some elements.
6. Summary
We’ve explored various entertaining ways to show off an array of elements.
Daniel Barczak
Daniel Barczak is a software developer with a solid 9-year track record in the industry. Outside the office, Daniel is passionate about home automation. He dedicates his free time to tinkering with the latest smart home technologies and engaging in DIY projects that enhance and automate the functionality of living spaces, reflecting his enthusiasm and passion for smart home solutions.
Leave a Reply