The Blog

How To Print An Array In Java?

How To Print An Array In Java?

Do you know how to print an array in Java? Have you ever thought what can be the standard procedures to print an array in Java? Let’s experience how to print an array in Java.

But before we start our programming journey, let’s think about the array in our daily life.

The array is just like a packet or a bag-like thing. In that packet, you can store some amount of the ingredients. But if you have to store different ingredients then? Let’s take an example:

Suppose, your mother provided you with 40 apples & 50 mangoes. And then asked you to drop those at your father’s office. In this what will you do? You have to carry those 90 ingredients to a distance. You will search for a packet or bag where you can keep those things.

Now, if you put all those things into one packet, it will be difficult for you to again distribute those. That is why you will try to get two bags. One is for apples & another is for mangoes. Then it will be easy for you.

The array is the same kind of thing. It is like a bag. You can put there any similar type of input. But we have to print those arrays from time to time. Similar to delivering those fruits to your father.

For that purpose, we need to know how to print an array in Java.

But, is there any way available to print an array in Java? Yes! There are.

What is Array in Java? Know More

Like other programming languages, Java array can only be able to store similar types of data. This means, that if the programmer declares an array as an integer array, then it can only able to accept the integer value. We can’t able to store the character to a certain array.

In Java, there are two types of arrays present. One is the One Dimension array & another is Multi dimension Array. One Dimension Array means it can only able to store a single line of data. Multidimensional Array means it can able to store multi-line information.

An array is different from the List. A list can able to store different types of data in a single list. But the array is only meant to store a similar type of data.

how to print an array in java

Types of Java Array Methods:

After knowing the basics of Java array, now it is time to know how to print an array in Java. For that purpose, there are some Java array methods present. Some apply to One Dimension Array & some for Multidimensional Array. Let’s make a list of those methods:

  1. Using Loop Method
  2. Converting To String Method
  3. Converting To Deep String Method

Let’s look at each method one by one.

Print an Array in Java Using Loop Method:

Here, we have to declare a One-Dimensional Array. We have to provide the values in it.

Then we have to print the values of the array using the For Each Loop. Also, here we added a space after every value print.

It will print all the elements in the array.

Example:

public class Main

{

              public static void main(String[] args)

              {

                            // Declaring One Array

                             int[] a = {10,20,30,40,50};

                             // Running For Each Loop

                             for(int i: a)

                             {

                                           // Printing Each Value With Space

                                           System.out.print(i+" ");

                             }

              }

}

Let’s look at the output of the above code. Hence, we come to know how to print an array in Java.

Output:

array in java using loop

If we want, we can able to use it normally for loop also. For that case, we have to declare a One Dimension Array. Also, we have to provide the values to it. Then we have to declare another variable that will accept the length of the array.

Then we have to run the For Loop until the end of the array. We have to print the array with a space after every element.

Example:

public class Main

{

              public static void main(String[] args)

              {

                             // Declaring One Array

                             int[] a = {10,20,30,40,50};

                             // Taking The Length Of The Array

                             int i, j=a.length;

                            for(i=0; i<j; i++)

                            {

                                           // Printing Each Value With Space

                                           System.out.print(a[i]+ " ");

                            }

              }

}

Let’s look at the output of the above code. Hence, we come to know how to print an array in Java.

Output:

array in java using loop

Print an Array in Java Converting to String Method:

This method applies to One Dimensional Array. For implementing this method, we have to import a package of Java. This package is used to do modifications with the array easily. The package is java.util.Arrays. it is a very important package. Whenever there is a need to modify an array to another structure, we have to use this package.

Again, we have to declare one array & provide the inputs to it. Then we have to use a method that will directly convert it to the string. It is one line of the method. But the problem is that it will not convert it to the general string. It will convert it to the List structure. In this case, we can find two brackets at two ends of the array. These brackets will sometime cause a problem for the programmers.

General Syntax: Arrays.toString(arrayname)

Example:

import java.util.Arrays;

public class Main

{

              public static void main(String[] args)

              {

                            // Declaring One Array

                             int[] a = {10,20,30,40,50};

                             // Converting To String or List From Array

              System.out.println(Arrays.toString(a));

              }

}

Let’s look at the output of the above code. Hence, we come to know how to print an array in Java.

Output:

array in java using converting to string method

Print an Array in Java Using Deep String Method:

This method is only used for Multidimensional Arrays. In this case, again, we have to import the package in Java. Here, in this case, also, we are converting the array to some other structure. The package which is going to import is java.util.Arrays.

In this case, also the string will not be the traditional string we are used to seeing. It will be a List like structure. In this structure, there will be two brackets at two ends of the structure. Also, this structure will make the multidimensional array an acceptable form. This form helps to do more work easily.

In this case, we have to declare one multidimensional array. Then we have to provide the data to the array. Then we will use this method. This method is a single-line method. It is called Deep String Method, as it helps to convert the multidimensional array into a big string. It will be a very helpful method for multidimensional arrays.

General Syntax: Arrays.deepToString(arrayname)

Example:

import java.util.Arrays;

public class Main

{

              public static void main(String[] args)

              {

                            // Declaring One Array

                             int[][] a = {{10,20},{30,40},{50}};

                             // Converting To String or List From Multi dimension Array

                             System.out.println(Arrays.deepToString(a));

              }

}

Let’s look at the output of the above code. Hence, we come to know how to print an array in Java.

Output:

array in java using deep string method

Conclusion:

As we saw printing an array in Java is very important.

It is a very important feature that helps to provide the output in a good manner form.

It is advisable to use the Loop method instead of converting it to the string method. It will create trouble with the brackets available at the ends of the output.

Also, if you guys are looking to get help with Java Assignments or Programming then you can use our Java Homework Help Services.

If you are looking for Final Year Java Project Ideas then you can contact us as well.

Sanjana Grover
Sanjana Grover

Leave a Comment