In this article, we will discuss printing an array in java using different methods like using loop, without loop, 2d array, and array in reverse with source code and output of that.
#1. Printing an Array in Java using for loop
A. Explanation of the source code
The code snippet below demonstrates how to print an array in Java using a for loop. The for loop iterates through the elements of the array and prints them out one by one.
int[] myArray = {1, 2, 3, 4, 5}; for (int i = 0; i < myArray.length; i++) { System.out.print(myArray[i] + " "); }
B. Output
The output of the code would be: 1 2 3 4 5
C. Example
This code could be used to print out the elements of an array of integers, such as the prices of items in a shopping cart.
#2. Printing an Array in Java without a Loop
A. Explanation of the source code
The code snippet below demonstrates how to print an array in Java using the Arrays.toString() method, which converts the array to a string and then prints it out.
int[] myArray = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(myArray));
B. Output
The output of the code would be: [1, 2, 3, 4, 5]
C. Example
This code could be used to quickly print out the elements of an array, without the need for a loop.
#3. Printing an ArrayList in Java
A. Explanation of the source code
The code snippet below demonstrates how to print an ArrayList in Java using a for-each loop. The for-each loop iterates through the elements of the ArrayList and prints them out one by one.
ArrayList<Integer> myArrayList = new ArrayList<Integer>(); myArrayList.add(1); myArrayList.add(2); myArrayList.add(3); for (int element : myArrayList) { System.out.print(element + " "); }
B. Output
The output of the code would be: 1 2 3
C. Example
This code could be used to print out the elements of an ArrayList of integers, such as a list of student IDs.
#4. Printing an Array in Reverse Order in Java
A. Explanation of the source code
The code snippet below demonstrates how to print an array in reverse order in Java using a for loop. The for loop iterates through the elements of the array in reverse order, starting from the last element, and prints them out one by one.
int[] myArray = {1, 2, 3, 4, 5}; for (int i = myArray.length - 1; i >= 0; i--) { System.out.print(myArray[i] + " "); }
B. Output
The output of the code would be: 5 4 3 2 1
C. Example
This code could be used to print out the elements of an array in reverse order, such as a list of historical events in reverse chronological order.
#5. Printing a 2D Array in Java
A. Explanation of the source code
The code snippet below demonstrates how to print a char array in Java using a for loop. The for loop iterates through the elements of the array and prints them out one by one.
char[] myCharArray = {'a', 'b', 'c', 'd', 'e'}; for (int i = 0; i < myCharArray.length; i++) { System.out.print(myCharArray[i] + " "); }
B. Output
The output of the code would be: a b c d e
C. Example
This code could be used to print out the elements of a char array, such as the letters of the alphabet.
#6. Printing a Byte Array in Java
A. Explanation of the source code
The code snippet below demonstrates how to print a byte array in Java using a for loop. The for loop iterates through the elements of the array and prints them out one by one.
byte[] myByteArray = {1, 2, 3, 4, 5}; for (int i = 0; i < myByteArray.length; i++) { System.out.print(myByteArray[i] + " "); }
B. Output
The output of the code would be: 1 2 3 4 5
C. Example
Please note that this code is an example of how each code snippet could be used in a real-world scenario. However, it’s worth mentioning that the examples given are just for demonstration and these codes can be used for various other purposes too.
In general, it’s important to keep in mind that when printing arrays in Java, it’s important to choose the most appropriate method based on the specific requirements of the task at hand. The for loop and for-each loop are useful for iterating through the elements of an array and performing operations on each element, while the Arrays.toString() method is useful for quickly printing out the entire array in a single line. Additionally, it’s important to keep in mind that when iterating through an array in reverse order, you should start the loop from the last element and decrease the index in each iteration.
Learn More:
- 4 Type Of Java Access Modifiers
- A Beginners Guide To Java-Based Machine Learning Libraries
- What Is Hibernate In Java With An Example
- Java Nested Class Overview Explained With Examples
- Java Abstract Class Overview Explained With Examples
- Java Interfaces For Beginners Explained With Examples
- Java Inheritance Types For Beginners With Examples
- What Is Iterator In Java? With Example
- Java Constructor For Beginners Explained With Examples
- What Is An Exception In Java?
- How To Throws Exception In Java With Example