
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to Write an Array of Strings to the Output Console in Java
We can write an array of strings to the output console in Java by using loops or built-in utility methods. The following are the ways to write an array of strings to the output console.
Using for-loop
Using Arrays.toString()
Nested Array
Using for-loop
A for loop iterates through the array and prints each element to the console. It repeats a code block for a specified number of iterations.
Syntax
The following is the syntax for using for loop
for (statement 1; statement 2; statement 3) { // code block to be executed }
Example
The following example demonstrates writing elements of an array to the output console through looping
public class Welcome { public static void main(String[] args) { String[] greeting = new String[3]; greeting[0] = "This is the greeting"; greeting[1] = "for all the readers from"; greeting[2] = "Java Source ."; for (int i = 0; i < greeting.length; i++){ System.out.println(greeting[i]); } } }
Output
This is the greeting For all the readers From Java source .
Using Arrays.toString()
The java.util.Arrays.toString(int[]) method provides a string representation of the elements in the specified int array. This representation includes the array's elements enclosed in square brackets ("[]"), with adjacent elements separated by ", " (a comma and a space).
Example
The following example demonstrates writing elements of an array to the output console using Arrays.toString()
import java.util.Arrays; public class HelloWorld { public static void main(String[] args) { String[] arr = new String[] {"Tutorials", "Point", ".com"}; System.out.println(Arrays.toString(arr)); } }
Output
[Tutorials, Point, .com] .
Using Nested Array
The Arrays.deepToString() method is used for nested arrays (arrays within arrays). It returns a string representation of multidimensional arrays, ensuring that each sub-array is properly represented, with brackets around each array and all elements displayed.
Example
The following example demonstrates writing elements of an array to the output using Arrays.deepToString() method
import java.util.Arrays; public class HelloWorld { public static void main(String[] args) { String[][] deepArr = new String[][] {{"Sai", "Gopal"}, {"Pallavi", "Priya"}}; System.out.println(Arrays.toString(deepArr)); System.out.println(Arrays.deepToString(deepArr)); } }
Output
[[Sai, Gopal], [Pallavi, Priya]]