Wednesday 19 April 2023

How Many ways we can print Array in Java

 

Hi Friends,

Today i am going to show how many ways we can print Array in Java.

As we know following points:- 

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.

The index of an array starts from 0. 


there are following ways to print array:


Java for loop

Java for-each loop

Java Arrays.toString() method

Java Arrays.deepToString() method

Java Arrays.asList() method

Java Iterator Interface

Java Stream API


package comm.neesri.collection;

 

import java.util.Arrays;

import java.util.Iterator;

import java.util.List;

 

public class PrintArray {

          public static void main(String[] args) {

 

                    // 1. ====> Java for loop

                    /*

                     * we have not provided the size of the array. In this case, the Java compiler

                     * automatically specifies the size by counting the number of elements in the

                     * array (i.e. 7).

                     */

                    int[] numbers = { 4, 1, 6, 2, 6, 9, 8 };

 

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

                              System.out.println("printing array through for loop " + numbers[i]);

 

                    }

 

                    // 2 ==> Java for-each loop

 

                    for (int numbers1 : numbers) {

                              System.out.println("printing array through for-each loop " + numbers1);

 

                    }

                    // 3. ==> Arrays.toString() method

 

                    System.out.println("printing array through Arrays.toString() method " + Arrays.toString(numbers));

 

                    // 4. ==> Arrays.asList() method

                    /*

                     * We have changed the type to Integer from int, because List is a collection

                     * that holds a list of objects. When we are converting an array to a list it

                     * should be an array of reference type.

                     *

                     * Java calls Arrays.asList(intArray).toString() . This technique internally

                     * uses the toString() method of the type of the elements within the list.

                     */

                    Integer[] numbers1 = { 4, 1, 6, 2, 6, 9, 8 };

                    System.out.println("printing array through Arrays.asList() method" + Arrays.asList(numbers1));

 

                    // 5. ==>iterator() method.

                    Integer[] numbers2 = { 4, 1, 6, 2, 6, 9, 8 };

                    // creating a List of Integer

 

                    List<Integer> list = Arrays.asList(numbers2);

                    Iterator<Integer> itr = list.iterator();

                    while (itr.hasNext()) {

 

                              System.out.println("printing array " + " iterator " + itr.next());

                    }

                    // 6. ==> stream() method

                    /*

                     * The Stream API is used to process collections of objects. A stream is a

                     * sequence of objects. Streams don’t change the original data structure, they

                     * only provide the result as per the requested operations.

                     *

                     */

                    int[] numbers3 = { 4, 1, 6, 2, 6, 9, 8 };

                    Arrays.stream(numbers3).forEach(System.out::println);

 

          }

 

}

 







No comments:

Post a Comment