# converting array of doubles to a List in order to find index of entry

• 12-20-2013, 07:06 PM
math8
converting array of doubles to a List in order to find index of entry
My main goal is to easily find the index of an entry in an array of doubles. I know that with an List, I can use the method indexOf(). So I am trying to first convert my Array of doubles into a List, then use the method indexOf on this newly created List. However, this does not work. It seems like the List I am creating contains an array as opposed to containing doubles. For instance:

Code:

```double[] a = {4.5, 2.0, 8.95}; java.util.List list_a = Arrays.asList(a); System.out.println(list_a.indexOf(8.95));```
The piece of code above is printing -1(as if 8.95 is not part of the List list_a) as opposed to printing 2.

However, if instead I use, the code below, everything works just fine.
Code:

```Object[] a = new Object[]{"4.5","2.0","8.95"}; java.util.List list_a = Arrays.asList(a); System.out.println(list_a.indexOf("8.95"));```
My question is, how can I use an array of doubles and still be able to convert it to a List or to an ArrayList of doubles and NOT a List containing an array.
• 12-20-2013, 07:50 PM
Norm
Re: converting array of doubles to a List in order to find index of entry
why not write a simple method with a loop to search the array. Converting to a List and searching the list will be very expensive in terms of computer execution time.
• 12-20-2013, 08:53 PM
JosAH
Re: converting array of doubles to a List in order to find index of entry
The Arrays.asList( ... ) method needs an array of Objects of some sort; a double isn't an Object (but a String is). Make your array an array of Double type elements (with a capital D) and everything works fine.

kind regards,

Jos
• 12-20-2013, 09:17 PM
Norm
Re: converting array of doubles to a List in order to find index of entry
If there are many lookups to be done on an array that is not being updated, sort it and use Arrays class's binarySearch() method.