Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to print array in java

Tags: print array loop

 To Print an array in Java, you can use a for loop to iterate through the array and print each element. Here is an example of how to print an array of integers:

Here is a simple program that explains how to print an array of integers in java

  1. int[] myArray = {1, 2, 3, 4, 5};
  2. for (int i = 0; i
  3.     System.out.print(myArray[i] + " ");
  4. }

This will print the following output: "1 2 3 4 5".

Alternatively:  Arrays.toString(myArray) and directly print the array.

System.out.println(Arrays.toString(myArray));

This will also print the array in same format "1 2 3 4 5"

We can also use the enhanced for loop (also known as the "for-each" loop) to print an array in Java. This type of loop automatically iterates through each element in the array, making it more concise and easier to read than a traditional for loop:

  1. for (int element : myArray) {
  2.     System.out.print(element + " ");
  3. }

This will also print the same output as the previous example "1 2 3 4 5".

When printing arrays of other types, such as Strings or objects, we can use the same approach. For example, if you have an array of Strings, you can use the enhanced for loop to print each element:

This will print the following output: "Hello World".

It's worth noting that, the Arrays.toString() method will work for all types of arrays, including arrays of objects, it will call the toString() method of each element in the array.

In case of the custom class objects, you may need to override the toString() method of the class.

Another way to print an array in Java is to use the Arrays.deepToString() method if the array is a multi-dimensional array. This method is similar to Arrays.toString(), but it can handle multi-dimensional arrays and it will recursively call itself for each sub-array. Here's an example of how to use Arrays.deepToString() to print a 2D array:

  1. int[][] my2DArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  2. System.out.println(Arrays.deepToString(my2DArray))

This will print the following output: "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]".

we can also use the java.util.Arrays.stream() method to print an array in Java 8 and later versions. This method returns a stream of the array's elements, which can then be passed to various stream operations such as forEach(). Here's an example of how to use Arrays.stream() to print an array:

int[] myArray = {1, 2, 3, 4, 5};

Arrays.stream(myArray).forEach(System.out::print);

This will also print the same output as the previous examples "1 2 3 4 5".

It's worth noting that when you use the Arrays.stream() method to print an array, it does not add any spaces or newlines between the elements, unlike the for loop and enhanced for loop. If we want to add spaces or newlines, you can use the map() method to transform each element into a string, and then use the forEach() method to print each string.



This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

How to print array in java

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×