Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Java Program to Print Array Elements

by suresh

Write a Java Program to Print Array Elements. Alternatively, write a Java program to Print Elements in an Array using For Loop, While Loop, and Functions with n example of each.

Java Program to Print Array Elements using For Loop

This program in Java allows the user to enter the Size and elements of an Array. Next, we are using For Loop to iterate each element in this array, and print those array elements.

// Java Program to Print Array Elements using For Loop
import java.util.Scanner;

public class PrintArray1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i, Number;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Number = sc.nextInt();	
		
		int [] Array = new int[Number];
		
		System.out.print(" Please Enter " + Number + " elements of an Array  : ");
		for (i = 0; i < Number; i++)
		{
			Array[i] = sc.nextInt();
		}     
	 
		System.out.println("\n **** Elements in this Array are  **** ");
		for (i = 0; i < Number; i++)
		{
			System.out.print(Array[i] + "\t");
		}
	}
}
Java Program to Print Array Elements 1

Here, Java For Loop make sure that the number is between 0 and maximum size value. In this example, it is from 0 to 7

for(i = 0; i < Size; i ++)

First Iteration: for (i = 0; 0 < 6; 0++)
Condition is True. So, the compiler prints the first element(6)  in this Array

Second Iteration: for (i = 1; 1 < 6; 1++)
Condition is True – compiler print the second element (15)

Java For Loop Third Iteration: for (i = 2; 2 < 6; 2++)
Condition is True. So, the Java compiler print the third second element (25)

Fourth Iteration: for (i = 3; 3 < 6; 3++)
Condition is True. So, it prints the fourth element (35)

Fifth Iteration: for (i = 4; 4 < 6; 4++)
Condition is True. So, the Java compiler print fourth element (45)

Sixth Iteration: for (i = 5; 5 < 6; 5++)
Condition is True. So, it prints the fourth element (55)

Seventh Iteration: for (i = 6; 6 < 6; 6++)
Condition is False. So, compiler exits from the for loop

Java Program to Print Array Elements using While Loop

This program is the same as above, but this time we used the While Loop to print array elements

// Java Program to Print Array Elements using While Loop
import java.util.Scanner;

public class PrintArray2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i = 0, j = 0, Number;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Number = sc.nextInt();	
		
		int [] Array = new int[Number];
		
		System.out.print(" Please Enter " + Number + " elements of an Array  : ");
		while (i < Number)
		{
			Array[i] = sc.nextInt();
			i++;
		}     
	 
		System.out.println("\n **** Elements in this Array are  **** ");
		while (j < Number)
		{
			System.out.print(Array[j] + "\t");
			j++;
		}
	}
}
Java Program to Print Array Elements 2

Java Print Array Elements using Functions

This Java program to display array elements is the same as the first example. However, we used a separate Java method to place the logic for displaying array elements.

// // Java Program to Print Array Elements using Functions
import java.util.Scanner;

public class PrintArray3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i, Number;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Number = sc.nextInt();	
		
		int [] Array = new int[Number];
		
		System.out.print(" Please Enter " + Number + " elements of an Array  : ");
		for (i = 0; i < Number; i++)
		{
			Array[i] = sc.nextInt();
		}     
		printAttay(Number, Array);
	}
	
	public static void printAttay(int Number, int[] Array)
	{
		int i;
		
		System.out.println("\n **** Elements in this Array are  **** ");
		for (i = 0; i < Number; i++)
		{
			System.out.println(" Element at Array["+ i +"] = " + Array[i]);
		}
	}
}
Java Program to Print Array Elements 3

Java Print Array Elements using Recursive Methods

This Java program is the same as the above. But this time, we are calling the Print Array elements method Recursively with updated values. 

// Java Program to Print Array Elements using Recursive Method
import java.util.Scanner;

public class PrintArray4 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i, Number;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Number = sc.nextInt();	
		
		int [] Array = new int[Number];
		
		System.out.print(" Please Enter " + Number + " elements of an Array  : ");
		for (i = 0; i < Number; i++)
		{
			Array[i] = sc.nextInt();
		}     
		printAttay(Array, 0, Number);
	}
	
	public static void printAttay(int[] Array, int Start, int Size)
	{
		if(Start >= Size)
		{
			return;
		}
		
		System.out.println(" Element at Array["+ Start +"] = " + Array[Start]);
		printAttay(Array, Start + 1, Size);
	}
}
Java Program to Print Array Elements 4

Placed Under: Java Programs

  • Java Hello World Program
  • Java Add Two Numbers Program
  • Java Compound Interest Program
  • Java Number Divisible by 5 & 11
  • Java Cube of a Number Program
  • Java Print Even Numbers 1 to N
  • Java GCD of Two Numbers
  • Java LCM of Two Numbers
  • Java Largest of Two Numbers
  • Java Largest of Three Numbers
  • Java Print Multiplication Table
  • Java Odd Numbers from 1 to N
  • Java Odd Even Program
  • Java find +Ve or -Ve number
  • Java Power of a Number Program
  • Java Calculate Profit or Loss
  • Java Print 1 to 100 without Loop
  • Java Quadratic Equation roots
  • Java Square of Number Program
  • Java Simple Interest Program
  • Java Sum of Even Numbers
  • Java Sum of Odd numbers
  • Java Sum of Even & Odd Number
  • Java find Total ,Average & Percentage of 5 Subjects
  • Java ASCII Value of a Character
  • Java ASCII Value of all Characters
  • Java String Comparison program
  • Java Area of Circle Program
  • Java Area Of Triangle Program
  • Java Area Of Trapezoid Program
  • Java Area of Equilateral Triangle
  • Java Area of Rectangle Program
  • Java Area of Right angle triangle
  • Java Volume & Surface of Sphere
  • Java Volume & Surface of Cone
  • Java Volume & Surface of Cuboid
  • Java Volume & Surface of Cube
  • Java Cylinder Volume & Surface
  • Java Math Library
  • Java Armstrong Number Program
  • Java Perfect Number Program
  • Java Palindrome Program
  • Java Count Digits in a Number
  • Java Calculate Electricity Bill
  • Java Find Factors of a Number
  • Java Factorial Program
  • Java Fibonacci Series program
  • Java find First Digit of a Number
  • Java find Last Digit of a Number
  • Java First & Last Digit of Number
  • Java Generic Root of Number
  • Java Natural Numbers in Reverse
  • Java Natural Numbers from 1 to N
  • Java Find Sum of Prime Numbers
  • Java Print Prime Numbers 1 to N
  • Java Prime Number Program
  • Java Reverse a Number Program
  • Java Strong Number Program
  • Java Sum of N Natural Numbers
  • Java Sum of Digits Program
  • Java Swap Two Numbers
  • Java Leap Year Program
  • Java Number of Days in a Month
  • Java Array
  • Java Array Arithmetic Operations
  • Java Program to Copy an Array
  • Java Count Array Duplicates
  • Java count even number in array
  • Java count odd numbers in Array
  • Java Count Even & Odds in Array
  • Java Count Negative Array Items
  • Java Count Positive Array Items
  • Java Count Array +Ve & -Ve num
  • Java Delete Array Duplicates
  • Java Large & Small Array Item
  • Java Largest Array Number
  • Java Print Array Elements
  • Java Print Positive Array Items
  • Java Print Negative Array Items
  • Java Smallest Array Number
  • Java Second Largest Array Item
  • Java Print Unique Array Items
  • Java +Ve & -Ve in separate array
  • Java Sort Array in Descending
  • Java Sort Array in Ascending
  • Java Sum of Array Elements
  • Java Sum of Array Even Numbers
  • Java Sum of Array Odd Numbers
  • Java Sum of Array Even and Odd
  • Java Swap Arrays without temp
  • Java Matrix Introduction
  • Java Print Matrix items
  • Java Check 2 Matrices are Equal
  • Java Matrix Arithmetic operation
  • Java Matrix Addition
  • Java Matrix Multiplication
  • Java Find Matrix Determinant
  • Java Identity Matrix
  • Java Interchange Matrix diagonal
  • Java Matrix Lower Triangle
  • Java Matrix Lower Triangle Sum
  • Java Matrix Upper Triangle
  • Java Matrix Upper Triangle Sum
  • Java Scalar Matrix Multiplication
  • Java Sparse Matrix
  • Java Sum of Matrix Diagonal
  • Java Sum of Matrix Opp Diagonal
  • Java Sum of each Matrix Row
  • Java Sum of each Matrix Column
  • Java Sum of Matrix row & column
  • Java Symmetric Matrix
  • Java Transpose Matrix
  • Java Rectangle Star Pattern
  • Java Square Star Pattern
  • Java Hollow Box Number Pattern
  • Java Box Number Pattern of 1, 0
  • Java Square Number Pattern
  • Java Print Floyd’s Triangle
  • Java Print String Characters
  • Java String replace 1st Char Occ
  • Java String replace last Char occ
  • Java String remove 1st Char Occ
  • Java string remove last Char Occ
  • Java String remove 1st, last char
  • Java String remove all char occur
  • Java String Find 1st Char Occur
  • Java String find all char occur
  • Java String Find Last Char Occur
  • Java String Print 1st & last Char
  • Java String Chars ASCII values
  • Java Program to Concat Strings
  • Java String Characters count
  • Java Count Total Char Occur
  • Java Max Occur String Character
  • Java Toggle String Characters
  • Java Frequency of each Char
  • Java Count Vowels & Consonants
  • Java Count alph digits, special
  • Java Palindrome String
  • Java Program to Reverse a String
  • Java Min Occur String character
  • Java Convert Lower to Upper
  • Java Convert Upper to Lower
  • Java Count total String words

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy