RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Jul 1, 2021

Find Element Position in an Array with Java

This is an easy way to find the element position or location in an Array using the Java programming language. We use java.util.Arrays to achieve this. Try out the following code example:


import java.util.Arrays;

public class FindPosition {
	public static void main(String[] args) 
		FindPosition findPosition = new FindPosition();

	private void proceed()  
		int missingNumber = 4;
		int[] dataArr = new int[] { 3, 5, 6 }; //We have number 4 missing in this array
		//The assumption is that the data in dataArr will be sorted 
		int position = Arrays.binarySearch(dataArr, missingNumber); //4 is provided here so that we can figure out the position 
		int missingPosition = ~position;
		missingPosition++; //Java array position starts at position 0. But for human readable form. it starts at position 1. Hence ++
		System.out.print("Number " + missingNumber + " will have to inserted at positon : " + missingPosition);


This code provides us with the following output:

[root@mypc]# java FindPosition
Number 4 will have to inserted at positon : 2
Sridhar M S
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date