Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array function similar to Excel Match?  RSS feed

 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.techonthenet.com/excel/formulas/match.php

basically have an array and search it. And like the excel Match function, if the value is not found in the array it will return the index of the next closest value? This is something thats easy to code, but i was just tryin to see if there was a built in java method that would do something like this for me.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that I'm aware of. Well, not for arrays. SortedSet NavigableSet (with implementing class TreeSet) does have something like this, with the head and tail sets.

NavigableSet is a bit better than SortedSet because it allows the bound for the head / tail sets to be inclusive or not; with SortedSet you cannot specify this yourself.
 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
awesome man thanks that almost worked lol. i using strings of time and the set doesn't know the difference between AM and PM when comparing strings



always returns 11:30 PM. so ill have to do some data manipulation i guess and convert to military time or something before i insert the data into the set. but either way its a step in the right direction and better than writing my own function!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrrays#binarySearch can return a meaningful negative value when an exact match is not found.
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strings in your set will be sorted alphabetically so if you want to take into account the AM / PM you would need to use something other than String or have a class that implements comparable and overides compareTo() to take it into account i think.
 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool i got it, thanks again for showing me that NavigableSet!!

 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!