5 Different ways to print arrays in java

  •  Arrays in java are used to hold similar data types values.
  • System.out.print() method does not print values of array.
  • In order to print array values we have different ways.
  • Normally we use for loop and by using index we will print each element inside array.
  • let us see how many ways we can able to print values of array. 


1.Print array in java using for loop
  • How to print array in java using for loop?
  • Yes we can print arrays elements using for loop.
  • Find the length of the array using array.length and take initial value as 0 and repeat until  array.length-1. 
  • Then access each index values of an array then print.
#1. Write a program to print array in java using for loop

  1. package arraysinterview;
  2.  
  3. public class PrintArray {
  4.  
  5.     /**
  6.      * How to print java array using for loop
  7.      * @author www.instanceofjava.com
  8.      */
  9.     
  10. public static void main(String [] args){
  11.         
  12.         int[] array = { 12,13,8,34,2,7,9,43,54,21};
  13.         
  14.         for (int i = 0; i < array.length; i++) {
  15.             System.out.println(array[i]);
  16.         }
  17.         
  18. }
  19.  
  20. }

Output:

  1. 12
  2. 13
  3. 8
  4. 34
  5. 2
  6. 7
  7. 9
  8. 43
  9. 54
  10. 21

2. Print string array in java using Enhanced for loop

  • From java 1.5 using enhanced for loop also we can print array values.

#2.   How to print string array in java using for each loop



3. Using Arrays.toString(array) method
  • By  using Arrays.toString(array) method we can print array values.

#3 Write a program to print array in java using Arrays.toString(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.toString(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.         String[] array = { "hi", "hello", "java"};
  12.         
  13.         
  14.    System.out.println(Arrays.toString(array));
  15.                
  16.   }
  17. }

Output:

  1. [hi, hello, java]
4.Using Arrays.deepToString(array) Method

  • Arrays,deepToString(array) method added in java 5 with generics and varargs.

#4 Write a program to print array in java using  Arrays.deepToString(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.deepToString(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.         int[][] array = new int[][]{
  12.                 {1,2,3},
  13.                 {11,12,13},
  14.                 {4 ,5,6},
  15.                 };
  16.         
  17.   System.out.println(Arrays.deepToString(array));
  18.       
  19.                
  20.   }
  21. }

Output:
  1. [[1, 2, 3], [11, 12, 13], [4, 5, 6]]

5.Using Arrays.asList(array) Method

  • Arrays,asList(array) method we can print array elements

#5 Write a program to print array in java using  Arrays.asList(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.asList(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.       int[] array ={1,2,3,4,5,6};        
  12.      System.out.println(Arrays.asList(array));
  13.  
  14.      String[] strarray ={"hi","array","print"};
  15.       System.out.println(Arrays.asList(strarray));
  16.                
  17.   }
  18. }

Output:
  1. [[I@2a139a55]
  2. [hi, array, print]

Exception handling multiple choice interview questions and answers in java

Java objective type questions and answers on static keyword



6.What is the output of below program ?

java multiple choice questions with answers

x=10 x=10
x=10 x=20
x=30 x=30
x=30 x=20




Java mcq with answers on this keyword




Core java multiple choice questions with answers on method overloading




Java mutliple choice questions with answers on constructors

  • On the request of many friends we added java multiple questions with answers.
  • Java mcq questions with answers on constructors.
  • We will add all java topics multiple questions with answers for freshers and experienced.



Select Menu