Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element position in array

Tags:

java

arrays

I have an array:

char[] modifiers = {'A', 'M', 'D'};

and a variable:

char a = 'D'

How to get position of variable value in array?

Thanks

like image 653
AVi Avatar asked Nov 20 '10 20:11

AVi


2 Answers

This is very simple and tested code for your reference

String[] arrayValue = {"test","test1","test2"};
int position = Arrays.asList(arrayValue).indexOf("test");

position: 0th Position
like image 109
Manimaran Samuthirapandi Avatar answered Oct 28 '22 01:10

Manimaran Samuthirapandi


This is the shortest way I know. I had this as a comment but now writing it as an answer. Cheers!

Character[] array = {'A','B','D'};

Arrays.asList(array).indexOf('D');

like image 25
st0le Avatar answered Oct 28 '22 01:10

st0le