How to

How to print array in Java

0

What Are Java Arrays?

An array in Java is a set of variables referenced by using a single variable name combined with an index number. Each item of an array is an element. All the elements in an array must be of the same type. Thus, the array itself has a type that specifies what kind of elements it can contain. An int array can contain int values, for example, and a String array can contain strings.

Java array is a data structure where we can store the elements of the same data type. The elements of an array are stored in a contiguous memory location. So, we can store a fixed set of elements in an array.

There are following ways to print an array in Java:

  • Java for loop
  • Java for-each loop
  • Java Arrays.toString() method
  • Java Arrays.deepToString() method
  • Java Arrays.asList() method

1) Java for loop

It is used to execute a set of statements repeatedly until a particular condition is satisfied.

Syntax:

  1.    
  2. for(initialization; condition; increment/ decrement)  
  3. {  
  4. //statements  
  5. }  

Example of for loop

In the following example, we have created an array of length four and initialized elements into it. We have used for loop for fetching the values from the array. It is the most popular way to print array in Java.

  1. public class PrintArrayExample1  
  2. {    
  3. public static void main(String args[])  
  4. {    
  5. //declaration and instantiation of an array  
  6. int arr[]=new int[4];     
  7. //initializing elements   
  8. arr[0]=10;  
  9. arr[1]=20;    
  10. arr[2]=70;    
  11. arr[3]=40;    
  12. //traversing over array using for loop  
  13. for(int i=0;i<arr.length;i++)    //length is the property of the array  
  14. System.out.println(arr[i]);    
  15. }  
  16. }    

 

2) Java for-each loop

Java for-each loop is also used to traverse over an array or collection. It works on the basis of elements. It returns elements one by one in the defined variable.

Syntax:

  1. for(Type var:array)  

Example of for-each loop

In the following example, we have created an array of String type of length four and initialized elements into it. We have used for-each loop to traverse over the array.

  1. public class PrintArrayExample2  
  2. {  
  3. public static void main(String args[])   
  4. {  
  5. // declaration and instantiation of an array   
  6. String[] city = new String[4];  
  7. //initializing elements   
  8. city[0] = “Delhi”;  
  9. city[1] = “Jaipur”;  
  10. city[2] = “Gujarat”;  
  11. city[3] = “Mumbai”;  
  12. //traversing over array using for-each loop   
  13. for (String str : city)   
  14. {  
  15. System.out.println(str);  
  16. }  
  17. }  
  18. }  

3) Java Arrays.toString() method

Java Arrays.toString() is a static method of Arrays class which belongs to java.util package It contains various methods for manipulating array.

Syntax:

  1. public static String toString(int[] a)  

It accepts an array of any primitive type as an argument. It returns a string representation of an array that contains a list of array’s elements. The elements of an array are converted to String by String.valueOf(int) .

Example of toString() method

  1. import java.util.Arrays;  
  2. public class PrintArrayExample3  
  3. {  
  4. public static void main(String[] args)  
  5. {  
  6. //declaring and initializing array  
  7. int array[] = {34,-10, 56, -9, -33};  
  8. //returns string representation of the specified array  
  9. System.out.println(Arrays.toString(array));  
  10. }  
  11. }  

4) Java Arrays.deepToString() method

The deepToString() method of Java Arrays class is designed for converting multidimensional arrays to strings.

Syntax:

  1. public static String deepToString(Object[] a)  

It accepts an array as a parameter. It returns the String representation of an array.

Example of deepToString() method

In the following example, we have created a two dimensional array of float type.

  1. import java.util.Arrays;  
  2. public class PrintArrayExample4   
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. //declaration and initialization of two dimensional array of float type  
  7. float[][] array = {{1.2f, 2.5f}, {3.9f, 4.0f}, {5.3f, 6.2f}};  
  8. System.out.println(Arrays.deepToString(array));  
  9. }  
  10. }   

5) Java Arrays.asList() method

Java Arrays.asList() is a static method of Java Arrays class which belongs to java.util package. It act as a bridge between array based and collection based API.

Syntax:

  1. public static <T> List<T>asList(T…a)  

The method also provides an easy way to create a fixed-size list initialize to contain many elements.

  1. List<T> obj=Arrays.toString(array[] a  

It accepts an array as an argument. It returns the list view of an array.

Example of asList() method

  1. import java.util.Arrays;  
  2. public class PrintArrayExample5  
  3. {  
  4. public static void main(String [] args)  
  5. {  
  6. //declaration and initialization of two dimensional array  
  7. String[] stringArray={“Hello”,”Java”,”Programmers”};  
  8. System.out.println(Arrays.asList(stringArray));  
  9. }  

nikhildada

Best 20 Christian movies on Amazon Prime

Previous article

How to play Smash with friends online

Next article

You may also like

Comments

Leave a reply

Your email address will not be published.

More in How to