how to print an array in java

How To Print An Array In Java?

Are you curious to know how to print an array in Java? Have you ever thought what can be the standard procedures ? Let’s get to know about “printing an array in Java” to get all your doubts clear.

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 in a certain array.

In Java, there are two types of arrays present. One is the One Dimension array & another is the Multi Dimension Array. One Dimension Array means it can only able to store a single line of data. A 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. Still have no clue about the topic and you want to hire a Java programmer for you then you can contact us for the best Java help.

 

What Are The 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 Arrays & some to Multidimensional Arrays. Let’s make a list of those methods:

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

Let’s look at each method one by one.

 

How To 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.

 

How to print an array using loop method?

 

It will print all the elements in the array.

Example code :

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:

How to print 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:

How to print array in java using loop?

 

How To Print an Array in Java Converting to String Method?

 

This method applies to the 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 a 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:

How to print an array in java using converting to string method?

 

How To 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.

Also, you must be aware of comparing 2 strings in Java if you have covered Strings.

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 the 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:

How to print an 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.

If you are learning other programming languages like C++, Python then you can also learn about “How To Build A Max Heap From An Array Using C”?

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

You can also read these topics related to Java:

Java Project Help now

 

Leave a Comment

Your email address will not be published. Required fields are marked *