Java Program to Print an Array

What is an Array in Java ?
In Java, an array is a collection of similar type of elements, which can be of primitive data types like int, char, double, float, etc., or reference data types like objects or other arrays. An array is a container object that holds a fixed number of elements, which are stored in contiguous memory locations.
Ways to Print an Array in Java :
1. Using a for-each loop :
Syntax :
int[] myArray = {1, 2, 3, 4, 5}; for (int num : myArray) { System.out.println(num); }
2. Using for loop: :
Syntax :
int[] myArray = {1, 2, 3, 4, 5}; for (int num = 0; num < myArray.length ; num++) { System.out.println(myArray[num]); }
3. Using Arrays.toString() method:
Syntax :
int[] myArray = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(myArray));
4. Using Arrays.deepToString() method for multi-dimensional arrays:
Syntax :
int[][] myArray = {{1, 2}, {3, 4}, {5, 6}}; System.out.println(Arrays.deepToString(myArray));
5. Using Stream API:
Syntax :
int[] myArray = {1, 2, 3, 4, 5}; Arrays.stream(myArray).forEach(System.out::println);
Example 1 :
Run
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] myArray = {1, 2, 3, 4, 5}; // Using for loop: System.out.println("Using for loop:"); for (int i = 0; i < myArray.length; i++) { System.out.println(myArray[i]); } // Using for-each loop: System.out.println("Using for-each loop:"); for (int num : myArray) { System.out.println(num); } // Using Arrays.toString() method: System.out.println("Using Arrays.toString() method:"); System.out.println(Arrays.toString(myArray)); } }
Output :
Using for loop: 1 2 3 4 5 Using for-each loop: 1 2 3 4 5 Using Arrays.toString() method: [1, 2, 3, 4, 5]
Explanation :
This program declares an integer array myArray, and then prints its elements using the three methods we have mentioned earlier: for loop, for-each loop, and Arrays.toString() method. When you run this program, you will see the output of all three methods printed in the console.
Example 2 :
Run
import java.util.Arrays; public class Main { public static void main(String[] args) { int[][] arr = { {1, 2}, {3, 4}, {5, 6} }; System.out.println(Arrays.deepToString(arr)); } }
Output :
[[1, 2], [3, 4], [5, 6]]
Explanation :
In this example, we create a 2D array of integers with 3 rows and 2 columns. We then use the Arrays.deepToString() method to print the contents of the array.
Example 3 :
Run
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; Arrays.stream(arr).forEach(System.out::println); } }
Output :
1 2 3 4 5
Explanation :
In this example, we create an array of integers and use the Arrays.stream() method to convert it into a stream. We then use the forEach() method to print each element of the stream to the console.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment