Java Print Array Examples

1
28
print array elements in Java
Advertisement

 

Java print array example helps you to learn how to print array elements in java in different ways. We will help you to print array elements in a 2D (Two Dimensional) array also.

Print Array Elements in Java(one dimensional Array):

1.Using for while loop:

You can print the array elements using a while loop. The below example shows you to print the string array using a while loop.

public class While_Loop
{
  public static void main(String[] args) {
      
      String[] str = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
      
      int count = 0;
      
      while(count < str.length){
          
          System.out.println(str[count]);
          count++;
          
      }
    
  }
}

Output:

Sunday                                                                                                                                               
Monday                                                                                                                                               
Tuesday                                                                                                                                              
Wednesday                                                                                                                                            
Thursday                                                                                                                                             
Friday                                                                                                                                               
Saturday

2.Using for loop:

The second method to print array elements is using for loop. this is the very simplest method to print the array elements.below example show to print the string array using for loop.

public class For_loop
{
  public static void main(String[] args) {
      
      String[] str = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
      
      for(int i=0;i<str.length;i++){
          
          System.out.println(str[i]);
      }
    
  }
}

Output:

Sunday                                                                                                                                               
Monday                                                                                                                                               
Tuesday                                                                                                                                              
Wednesday                                                                                                                                            
Thursday                                                                                                                                             
Friday                                                                                                                                               
Saturday

3.Using enhanced for loop:

The enhanced forloop was introduced in Java 5 as a simpler way to iterate through all the elements of a Collection. It can also be used for arrays.

Enhanced forloops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the “standard” for loop should be preferred.

public class For_each_Loop
{
  public static void main(String[] args) {
      
      String[] str = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
      
      for(String s : str){
          
          System.out.println(s);
      }
    
  }
}

Output:

Sunday                                                                                                                                               
Monday                                                                                                                                               
Tuesday                                                                                                                                              
Wednesday                                                                                                                                            
Thursday                                                                                                                                             
Friday                                                                                                                                               
Saturday

4. Using Arrays Class in Java:

This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

The toString()returns the string representation of the contents of the specified array.

public static String toString(Object[] arr)

Parameters:

arr – the array whose string representation to return

Returns: a string representation of arr

Note: Need to import java.util.Arrays while using the Arrays class in Java.

import java.util.Arrays;

public class Arrays_Class
{
  public static void main(String[] args) {
      
      String[] str = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
      
      System.out.println(Arrays.toString(str));
    
  }
}

Output:

[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

5. Using String join method:

You can use join() method to print the array elements if you are using Java 8. join() method introduced in Java 8, this method is also useful if you want to concatenate the string elements.

 public static String join(CharSequence delimiter,CharSequence... elements)

Parameters:

delimiter – the delimiter that separates each element

elements – the elements to join together.

Returns: a new String that is composed of the elements separated by the delimiter

Throws:NullPointerException – If delimiter or elements is null

public class Main
{
  public static void main(String[] args) {
      
      String[] str = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
      
      System.out.println(String.join(" , ",str));
      
      
      // If you want to print each element in separate line the use the below Code
      
      /*
      
      System.out.println(String.join("\n",str));
      
         Output:
            Sunday                                                                                                                                               
            Monday                                                                                                                                               
            Tuesday                                                                                                                                              
            Wednesday                                                                                                                                            
            Thursday                                                                                                                                             
            Friday                                                                                                                                               
            Saturday 
      
      */
    
  }
}

Output:

Sunday , Monday , Tuesday , Wednesday , Thursday , Friday , Saturday

There are two more methods to print array elements in Java, but they are not quite popular. but still, you need to know how to use them.

6. Using java.util.stream class:

In order to use the stream class, you need to import java.util.stream;package in your code.

and 2nd thing, stream class introduced in Java 8 version, so you are running your code in the Java 8.

import java.util.*;

public class Main
{
  public static void main(String[] args) {
      
      String[] str = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
      
      Arrays.stream(str).forEach(System.out::println);
      
  }
}

Output:

Sunday                                                                                                                                               
Monday                                                                                                                                               
Tuesday                                                                                                                                              
Wednesday                                                                                                                                            
Thursday                                                                                                                                             
Friday                                                                                                                                               
Saturday
Print 2-D Array(Two dimensional Array):

Java print array example also helps you, if you are working with the two-dimensional array, you need to follow one of the given approaches below:

1.Using Nested for loop:

public class PrintTwoDimensionalArray
{
  public static void main(String[] args) {
      
      int[][] arr = {{1,2},{3,4}};
      
      for(int i=0;i<arr.length;i++){
          
          for(int j=0;j<arr[i].length;j++){
              
              System.out.println(arr[i][j]);
          }
      }
      
  }
}

Output:

1                                                                                                                                                    
2                                                                                                                                                    
3                                                                                                                                                    
4

2. Using Arrays Class in Java:

Arrays class has a very useful method to print the two-dimensional array in java. This method introduced in Java 5.

Returns a string representation of the “deep contents” of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.

public static String deepToString(Object[] arr)

Parameters:arr – the array whose string representation to return

Returns: a string representation of arr

Note: Need to import java.util.Arrays while using the Arrays class in Java.

import java.util.Arrays;

public class PrintTwoDimensionalArray
{
  public static void main(String[] args) {
      
      int[][] arr = {{1,2},{3,4}};
      
      System.out.println(Arrays.deepToString(arr));
      
  }
}

Output:

[[1, 2], [3, 4]]
You may also like:

 

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here