Results 1 to 4 of 4
  1. #1
    math8 is offline Member
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Default 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:

    Java 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.
    Java 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2013, 09:44 AM
  2. Replies: 5
    Last Post: 06-27-2012, 08:51 AM
  3. Replies: 10
    Last Post: 02-11-2011, 08:15 PM
  4. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •