In the previous article, we have seen Java Program to Reverse Array Elements
In this article we are going to see how we can print the elements of an array in various ways in Java.
Java Program to Print the Elements of an Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to print an array.
- Print Array Elements Using For Loop
- Print Array Elements Using For-each Loop
- Print User Input Array Elements Using For Loop
- Print Array Elements Using Arrays.toString()
- Print String Array Elements Using Arrays.asList()
- Print String Array Elements Using Java iterator interface
- Print Array Elements Using Java Stream API
Method-1: Print Array Elements Using For Loop
Approach:
- Use a for loop to iterate the array index.
- Print the array elements at those index.
Program:
public class array { public static void main(String args[]) { // Array with elements int arr[] = {10,15,1,30,50,7,1,0,0}; int row; System.out.print("The array elements are : "); //Loop to print the elements for(row=0;row<arr.length;row++) { System.out.print(arr[row]+" "); } } }
Output: The array elements are:10 15 1 30 50 7 1 0 0
Method-2: Print Array Elements Using For-each Loop
Approach:
- Use a for-each loop to iterate the elements in the variable.
- Print the variables.
Program:
public class array { public static void main(String args[]) { // Array with elements int arr[] = {10,15,1,30,50,7,1,0,0}; System.out.print("The array elements are : "); //For-each Loop to print the elements for(int iter:arr) { System.out.print(iter+" "); } } }
Output: The array elements are : 10 15 1 30 50 7 1 0 0
Method-3: Print User Input Array Elements Using For Loop
Approach:
- Ask the user for array length.
- Store it in a variable.
- Use a for loop to iterate the index and insert the elements.
- Use a for loop to print all the elements.
Program:
import java.util.Scanner; public class array { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Array with elements int arr[] = null; System.out.println("Enter the length of the array : "); int length = scan.nextInt(); arr = new int[length]; int iter; // Entering the array elements System.out.println("Enter the array elements : "); for(iter=0;iter<arr.length;iter++) arr[iter]=scan.nextInt(); System.out.println("The array elements are : "); //For Loop to print the elements for(iter=0;iter<arr.length;iter++) { System.out.print(arr[iter]+","); } } }
Output: Enter the length of the array : 5 Enter the array elements : 1 2 3 4 5 The array elements are : 1,2,3,4,5,
Method-4: Print Array Elements Using Arrays.toString()
Approach:
- Take an array with elements in it.
- Pass the array into the
Arrays.toString( )
function. - Print the generated string.
Program:
import java.util.Arrays; public class array { public static void main(String args[]) { // Array with elements int arr[] = {10,15,1,30,50,7,1,0,0}; //Printing using arrays.toString() library function System.out.print("The array elements are:"+Arrays.toString(arr)); } }
Output: The array elements are:[10, 15, 1, 30, 50, 7, 1, 0, 0]
Method-5: Print String Array Elements Using Arrays.asList()
Approach:
- Take a string array with elements in it.
- Pass the array into the
Arrays.asList( )
function. - Print the result.
Program:
import java.util.Arrays; public class array { public static void main(String args[]) { // String Array with elements String arr[] = {"New York","Brooklyn","Paris"}; //Printing using arrays.asList() library function System.out.print("The array elements are:"+Arrays.asList(arr)); } }
Output: The array elements are:[New York, Brooklyn, Paris]
Method-6: Print String Array Elements Using Java iterator interface
Approach:
- Take a Double array with elements in it.
- Create an iterator.
- Convert the array into a list.
- Use the iterator to iterate the list and print out the elements.
Program:
import java.util.Arrays; import java.util.Iterator; public class array { public static void main(String args[]) { // Double Array with elements Double arr[] = {10.3,20.5,35.3,50.5}; System.out.print("The array elements are : "); // Creating the iterator Iterator<Double> iter = Arrays.asList(arr).iterator(); // While loop that prints until there is no next element // The hasNext() checks whether there is a next element and returns a boolean value while(iter.hasNext()) { System.out.print(iter.next()+", "); } } }
Output: The array elements are : 10.3, 20.5, 35.3, 50.5,
Method-7: Print Array Elements Using Java Stream API
Approach:
- Take an array with elements in it.
- Pass the array into the
stream()
function and then use a for each loop with it to print out all the elements in it.
Program:
import java.util.Arrays; public class array { public static void main(String args[]) { // Array with elements Double arr[] = {10.3,20.5,35.3,50.5}; System.out.println("The array elements are"); // Prints using the stream API Arrays.stream(arr).forEach(System.out::println); } }
Output: The array elements are 10.3 20.5 35.3 50.5
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: