
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Print Even Positions Elements from an Array in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to find the array elements which are present in the even position in an array and print them.
A number is said to be even if it is divisible by 2 else it is called an odd number.
Let’s explore the article to see how it can be done by using Java programming language.
To Show You Some Instances
Instance-1
Suppose the original array is {12, 5, 77, 14, 91, 21, 1, 50}
After finding the even element position in an array, the result will be −
Even position of elements present in an array are: [77, 91, 1]
Instance-2
Suppose the original array is {12, 23, 11, 64, 5, 87, 22, 67, 100};
After finding the even element position in an array, the result will be −
Even position of elements present in an array are: [11, 5, 22, 100]
Instance-3
Suppose the original array is {11, 22, 33, 44, 55}
After finding the even element position in an array, the result will be −
Even position of elements present in an array are: [33, 55]
Algorithm
Step 1 − Declare and initialize an integer array.
Step 2 − Initialize for loop and check for its length.
Step 3 − Start the loop from 2 and add 2 to each index of an array till the last index.
Step 4 − Print the elements of the array.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm we have to find the array elements which are present in the even position in an array and print them.
public class Main{ //main method public static void main(String[] args){ //Declare and initialize the array elements int num[] = {12, 5 , 77, 14, 91, 21, 1}; System.out.println("Even position of elements present in an array are: "); //logic implementation for (int i = 2; i < num.length; i = i+2){ System.out.print(num[i]+" "); } } }
Output
Even position of elements present in an array are: 77 91 1
Approach-2: By Using User Defined Method
Example
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside the method as per the algorithm we have to find the array elements which are present in the even position in an array and print them.
public class Main{ //main method public static void main(String[] args){ int num[] = {12, 23, 11, 64, 5, 87, 22, 67, 100}; // calling the user defined method even_elements(num); } //method body public static void even_elements(int []num){ System.out.println("Even position of elements present in an array are: "); //logic implementation for (int i = 2; i < num.length; i = i+2){ System.out.print(num[i]+" "); } } }
Output
Even position of elements present in an array are: 11 5 22 100
In this article, we explored how to print the elements on an array which are present in even index positions by using Java programming language.
- Related Articles
- Print the Elements of an Array Present in Odd Positions in Java
- Java program to Print Odd and Even Number from an Array
- Java program to print odd and even number from a Java array.
- Python program to print the elements of an array present on even position
- Java Program to Print the Elements of an Array
- How to print the elements in a reverse order from an array in C?
- Create an object array from elements of LinkedList in Java
- How to print data of specific element from an array in java?
- How to Remove Even Numbers from Array in Java?
- Finding even length numbers from an array in JavaScript
- Count number of even and odd elements in an array in C++
- Positive elements at even and negative at odd positions (Relative order not maintained) in C++
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Print n smallest elements from given array in their original order
- How to pull even numbers from an array in MongoDB?
