Related Articles

Related Articles

Java Program to Write an Array of Strings to the Output Console
  • Last Updated : 29 Oct, 2020

We cannot print array elements directly in Java, you need to use Arrays.toString() or Arrays.deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.

In Java, arrays do not overwrite toString(). When we try write array directly to output console in Java, we get class_name + ‘@’ + hash_code of the array define by Object.toString(). See the below example for a better understanding. 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        String gfg[] = { "Geeks", "for", "Geeks" };
        System.out.println(gfg);
    }
}

chevron_right


Output

[Ljava.lang.String;@3d075dc0

Thus, to print Java array in a meaningful way, you don’t need to look further because your very own Collection framework provides lots of array utility methods in java.util.Arrays class. Here we have toString() method and deepToString() method to print array in Java.

Following are the methods to write an array of strings to the output console.



Method 1: Using Arrays.toString()

This method is used when you have one dimensional array.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
        String gfg[] = { "Geeks", "for", "Geeks" };
        System.out.println(Arrays.toString(gfg));
    }
}

chevron_right


Output

[Geeks, for, Geeks]

Above, we have used the Arrays.toString() method. Simply pass array name as argument in Arrays.toString() and all the elements of the array will get written in the output console.

Method 2: Using Arrays.deepToString()

This method is used when you have to two dimensional array.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
        String gfg[][]
            = { { "GeeksforGeeks", "Article Writing" },
                { "Google", "Search Engine" },
                { "Facebook", "Social Media" } };
        System.out.println(Arrays.deepToString(gfg));
    }
}

chevron_right


Output

[[GeeksforGeeks, Article Writing], [Google, Search Engine], [Facebook, Social Media]]

In the above example, we have used Arrays.deepToString() method. This method takes care of writing elements of two dimensional array to output console.

Method 3: Using for loop

In this method, we will access each element of the array and would write it to the output console.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        String gfg[] = new String[3];
        gfg[0] = "Geeks";
        gfg[1] = "for";
        gfg[2] = "Geeks";
        for (int i = 0; i <= 2; i++) {
            System.out.print(gfg[i] + " ");
        }
    }
}

chevron_right


Output

Geeks for Geeks 

In the above method, we have used for loop() method to access every element of gfg array and write it to the output console.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :