#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    UK
    Posts
    10
    Rep Power
    0

    Finding elements in an array


    I'm trying to find if a value exists as an element of an array. In PHP I'd use in_array(v,a) but I don't know what to use in JSP. Can anybody help please?
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    You can use Arrays.binarySearch(), but the array MUST be sorted beforehand or it won't work. Sorting the array just to search it probably wouldn't be very efficient. If you don't want to sort it you will have to roll-your-own search method.

    Depending on your needs, you may want to look into using some sort of Collection instead of an array. ArrayList, for example, has an indexOf(Object) method that will return to you the index of the specified object. HashMap has a method called containsValue(Object) that returns a boolean. Either of these would likely work for you.

IMN logo majestic logo threadwatch logo seochat tools logo