- Java Programming Examples
- 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 Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to write an array of strings to the output console in Java
Problem Description
How to write an array of strings to the output console?
Solution
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]); } } }
Result
The above code sample will produce the following result.
This is the greeting For all the readers From Java source .
Following example demonstrates writing elements of an array to the output console
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)); } }
The above code sample will produce the following result.
[Tutorials, Point, .com] .
Nested Array
Following example demonstrates writing elements of an array to the output console through looping.
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)); } }
The above code sample will produce the following result.
[[Sai, Gopal], [Pallavi, Priya]]
java_arrays.htm
Advertisements