• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

Java Program to Write an Array of Strings to the Output Console

October 31, 2023 //  by Talha Malik

The string array in Java is an important topic for dealing with real-world problems, especially for beginners. The string array contains a fixed number of strings. The strings are placed with respect to the index. The first string corresponds to the 0th index, the second string corresponds to the first index, and so on. When the strings are declared, different methods are applied to these arrays of strings as per the need.

In this article, we will go through different methods to print the array of strings to the output console in Java.

Java Program to Write an Array of Strings to the Output Console

The array of strings is written to the output console using different methods. It is to keep in mind that we can not print an array of strings directly to the output console. Therefore, the collection framework is used that implements different methods present in java.util.Arrays class that helps to print the string arrays in Java. Below is the list of multiple ways to print the output as an array of strings.

  • Method 1: Simplest Way Using for Loop
  • Method 2: Using Arrays.toString() Method(Single Dimensional Array)
  • Method 3: Using Arrays.deepToString() Method(Multi-Dimensional Array)
  • Method 4: Using for-each Loop
  • Method 5: Using Java Stream API

Method 1: Simplest Way Using for Loop

The most simple method to write a string array to the output console is by implementing the for loop. The for loop iterates over the strings in the array and outputs the results as per the input described in the code below.

public class Method1 {
  public static void main(String[] args) {
      //Declare the length of the array
      String[] s = new String[5];
      s[0] = "Java";
      s[1] = "needs";
      s[2] = "a lot";
      s[3] = "of";
      s[4] = "practice";
      System.out.println("The Elements of the Array are :" );
      //The for loop
      for (int x = 0; x < s.length; x++){
        System.out.println(s[x]);
      }
  }
}

In the above code block:

  • The class is declared as “Method1” to implement the “for loop”.
  • An object “s” is declared for the strings with its limit as “5”, if the elements exceed the limit, null is printed for all the exceeding strings at the output console.
  • The last step involves the implementation of a “for loop” to iterate over the elements and print the results accordingly.

Output

The output below shows that the strings are printed according to the index.

Method 2: Using Arrays.toString() Method (Single Dimensional Array)

This method is effectively implemented on the one-dimensional array. The elements of the array are printed by declaring this method and the results appear on the output console as depicted below.

//Import the packages required
import java.io.*;
import java.util.*;
//Declare the class
class Method2 {
public static void main(String[] args)
{      //Enter the array of strings
String str1[] = { "Java", "is", "fun", "to", "learn"};
//Print the output
System.out.println("The Array is: " + Arrays.toString(str1));
}
}

In the above Java code, the strings are declared as “str1” in the array and are printed on the console by applying the Arrays.toString() method.

Output

In the below output, the strings in the array [Java, is, fun, to, learn] are printed accordingly. 

Method 3: Using Arrays.deepToString() Method (Multi-Dimensional Array)

This method is used when the multi-dimensional array is required to be printed. This method contains arrays within an array thus referred to as “nested arrays” depicted in the code below.

//Import the packages required
import java.io.*;
import java.util.*;
//Declare the class
class Method3 {
public static void main(String[] args)
{
    //Declare the array of strings
    String s1[][] = { { "Programming Language", "Python" }, { "Database", "SQL" },{ "Course", "Networking" } };
System.out.println("The Array is: " + Arrays.deepToString(s1));
}
}

In the above Java code, we have declared three different arrays within an array by implementing the Arrays.deepToString() method of Java.

Output

The output is printed as three arrays of strings within a single array. 

Method 4: Using for-each Loop

This loop in Java iterates over each and every element declared in the array. Each element in the array is printed separately to the output console depicted in the code below.

public class Method4 {
  public static void main(String[] args) {
      //Declare the length of the array
      String[] s = new String[5];
      s[0] = "Java";
      s[1] = "needs";
      s[2] = "a lot";
      s[3] = "of";
      s[4] = "practice";
      System.out.println("The Elements of the Array are :" );      //The for-each loop of Java
      for (String str : s)  
{ 
System.out.println(str); 
}
}
}

Output

The array of strings is printed at the output console.

Method 5: Using Java Stream API

The Stream API in Java is considered as the sequence of objects that supports various operations. It consists of various methods and one of the methods is the stream() method. This method implemented below returns a sequential stream of the array.

//Import the packages required
import java.io.*;
import java.util.*;
//Declare the class
class Method5 {
public static void main(String[] args)
{
    //Declare the array of strings
    String s1[] = { "Programming Language", "Python" };
    //Two operations
Arrays.stream(s1).forEach(System.out::println); 
} 
}

In the above code, the stream() method contains the array of strings which is iterated over using for each loop of Java which contains “System.out” as a reference object and the println() method to print the output. One thing to note here is that the “println()” method is converted to an expression since it is passed after the member access operator(::) in Java.

Output

The output below shows that the array of strings “Programming Language” and  “Python” are printed as the output.

This article has explored various methods to print the string array to the output console in Java.

Conclusion

To write an array of strings to the output console, you can use Java loops like for loop and for-each Loop along with Arrays.toString(), or Arrays.deepToString() method. Moreover, you can use the stream() method of Java Stream API to write the array of strings to the output. This article has effectively utilized multiple methods to print the array of strings to the output console.

Category: Java

Previous Post: « Java Scanner nextInt() Method
Next Post: instanceof Operator in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Math.min() Method in Java?

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact