5 Ways To Print Arrays With Elements In Java - MasterInJava

5 Ways To Print Arrays With Elements In Java

Searching in your browser how to print array with elements in Java? Then you are on the right website. Stick with this post because in this article I will show you the 5  different ways in which you can print array with elements in Java. So, without any delay let's jump on to the first point:

1. Using Loop (Old Method)

5 Ways To Print Arrays With Elements In Java
          
It is the oldest method to print array with elements in Java. So, basically in this method, you have to write a "String" and give it a name.

For example, I named it "master" and then use square brackets"[]" which is then followed by an equal sign"=".

After the equal sign, put an open curly bracket '{' and type the four words: Apple, Grapes, Mango, Watermelon(the words can be of your choice) such that each word is in double inverted commas and are separated by commas.

Then close the curly '}' bracket which is then followed by a semi-colon. So the line is:
                           
                               String master[] = {"Apple","Grapes","Mango","Watermelon"};

If you directly want to print this line by using the output statement it will show an error. So, firstly you have to create a loop.

  • Write the word "for" and start parentheses "("
  • You have to write an integer "int" and give it a name(I named it "i") which is followed by an equal sign.
  • Type 0 and put a semi-colon. 
  • Then you have to write the integer name (in my case it is "i") and put a less than sign "<". 
  • Write the string name which you have written in the previous line( for me it is 'master') and put a full stop. 
  • Type the word "length" followed by a semicolon and type the integer name and then use the plus sign twice"++".
  • Close the parantheses ')' and then put a semi-colon. So, the line is :

                                                          for(int i = 0;i<master.length;i++);

Now you have to finally write the output statement line " System.out.println();" and within the parentheses, we have to type the string name and within square brackets, you have to type the integer name. So the line is:

                                                           System.out.println(master[i]);

Click on the run button.

Output:

                                                   Apples
                                                   Grapes
                                                   Mango
                                                 Watermelon

2. Using Arrays.asList() Method

5 Ways To Print Arrays With Elements In Java
          
This method is one of the best ways to print arrays with elements. Here, we don't need to use the loop statement.

In this method first, we have to write a String, name it(for me it was 'master'), and give it's value as we have done in the first method. As we don't have to write a loop statement we can directly proceed on to the output statement.

Type the statement "System.out.println();" and within the parentheses "()" type Arrays.asList() and within the parentheses of Arrays.asList type the string name and finally click on run option.So the line is:

                                                 System.out.println(Arrays.asList(master));

Output:

                                             [Apples, Grapes, Mango, Watermelon]
                 

3. Enhanced for Loop Method

5 Ways To Print Arrays With Elements In Java
This method is pretty much similar to the first method. Here, we have to create a String and name it and assign its value as we have done in the first method.

In the loop statement instead of creating an Integer "int", we have to create a String and give it a name(I choose the name  "masterinJava") which is then followed by a column sign ":" and then the String name of the previous line(the value for the string 'masterinJava' is same as the value of the string 'master'). So, the lines are :
                                             
                               String master[] = {"Apples","Grapes","Mango","Watermelon"};
                                                     for(String masterinJava : master);

For the last line, I type the output statement, and inside the parentheses, I type the name of the String which I had used in the loop statement. So the line is:
                                                       
                                                     System.out.println(masterinJava);


Output:


                                                   Apples
                                                   Grapes
                                                   Mango
                                                 Watermelon

4. Arrays.toString() Method

5 Ways To Print Arrays With Elements In Java

Arrays.toString() method can print arrays with elements (both primitive and object) in a one-dimensional array. It is one of the recommended methods to print array elements. To implement this method first you have to create an Integer "int" or String as I have shown you in the first point.

                                                                 int master[] = {1,2,5};

 And write the output statement "System.out.println();" and within the parentheses write the Integer or String name(in my case, it is "master") and click on run.

Output:


                                                                              [1, 2, 5] 

5. Arrays.deepToString() Method

5 Ways To Print Arrays With Elements In Java

Arrays.deeptoString() method is a method that prints the array element from two dimensional to a multidimensional array. It is also one of the recommended methods.

To implement this method first we have to create a two-dimensional array. To create this we need to create a new Integer "int" and then use the square brackets "[]" twice which is followed by the name of the integer (I named it "inttwo") and put an equal sign "=".

Then we need to type "new int" which is followed by two square brackets"[]" and then type {{17,13,15}}. The numbers can be of your own choice. So the line:
                                               int [] [] inttwo = new int [] [] {{17,13,15}};

For the last line, we type the output statement "System.out.println();" and within the parentheses, we need to type "Arrays.deeptoString(inttwo);. So the line is:

                                         System.out.println(Arrays.deeptoString(inttwo));

 Finally, click on the run option.

Output:

                                                                    [[17, 13, 15]]

Conclusion

These were the 5 different ways of how to print an array with elements in Java including the oldest to the most recommended method. Now, go on and try it out. And if you found this post useful please do like and comment. Also share it with your friends so that they could learn the same. Also do read my previous post.


Post a Comment

0 Comments