All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Print A String Array In Java

Last Updated : Mar 11, 2024

How To Print A String Array In Java

In this article we will show you the solution of how to print a string array in java, when dealing with collections with text data, it's frequently necessary to print a string array in Java. In order to manage and modify a collection of textual information, string arrays are used to hold numerous strings within a single variable.

For debugging, showing results, or just giving users information, printing a string array's contents is essential.

Java requires iterating through each string's element and displaying it separately in order to print a string array.

Loops or array-specific built-in techniques can be used to do this.

The strings can be generated in the format of your choice, such as on different lines and separated by a delimiter, by progressively reading every element of the array.

Now we'll discuss the concept of printing an array of strings in Java.

Step By Step Guide On How To Print A String Array In Java :-

import java.util.Arrays;
public class PrintStringArray {
    public static void main(String[] args) {
        // Sample string array
        String[] names = {"Naman", "Gagan", "Chagan", "Aman", "Raman", "Suman"};
        // Printing the string array using a for loop
        System.out.println("Result:");
        for (int s = 0; s < names.length; s++) {
            System.out.println(names[s]);
        }
        // Printing the string array using an enhanced for loop (for-each loop)
        System.out.println("\nResult:");
        for (String name : names) {
            System.out.println(name);
        }
        // Printing the string array using the Arrays class
        System.out.println("\nResult:");
        System.out.println(Arrays.toString(names));
        // Printing the string array using a custom method
        System.out.println("\nResult:");
        printArray(names);
    }
    /**
     * Custom method to print the elements of a string array
     *
     * @param array The string array to be printed
     */
    private static void printArray(String[] array) {
        System.out.println("Result:");
        for (int s = 0; s < array.length; s++) {
            System.out.println("Result " + s + ": " + array[s]);
        }
    }
}
  1. You can see that in this example, we create the Java code that demonstrates how to print a string array.
  2. To use the Arrays class and its methods, the import statement import java.util.Arrays; is used at the beginning of the code.
  3. The main method, which acts as the program's entry point, is then declared in the PrintStringArray class.
  4. We define and initialize a names string array with sample names inside the main procedure.
  5. The string array is then printed using a for loop.
  6. Using the index variable i, we cycle through the array, printing each entry using System.out.println(names[i]).
  7. We then use an improved for loop (for-each loop) to output the string array. By directly referencing every element of the array without the need of an index variable, this loop streamlines the code.
  8. We use the Arrays.toString() function to print the string array.
  9. The entire array is transformed into a string representation using this Arrays class method, which we can then output using System.out.println().
  10. We also present a special technique called printArray().
  11. This method takes a string array as an input and iterates across the array while outputting each element's index using a for loop.
  12. Finally, we use the printArray() method to show a different method for printing the string array that uses a unique strategy.

Conclusion :-

As a result, we have successfully mastered the Java notion of printing a string array.

We also discovered how to quickly show the items of an array using the Arrays class, a for loop, and an extended for loop.

A custom method also gives you the freedom to alter the output format.

For debugging, presenting results, or providing information to users, we can quickly print the string array using these techniques.

I hope this article on how to print a string array in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪