Click to See Complete Forum and Search --> : ArrayList & Searching


lior6543
January 22nd, 2005, 02:43 PM
Hello!

I'm using ArrayList variable and I would like to use the Binary search function (or anything else) but my problem goes like this:

Each element in the array is a Class I created which holds several of variables in it. I would like to search an element in the array by a specific variable inside the Class (could be String*, int ... ) and get the index of the element as a return value.

Now how the **** can I do that?

atkin
January 24th, 2005, 07:40 AM
You should be using a structure if you need to do that. You should be not accessing variables from class. You may want to rethink you design

lior6543
January 24th, 2005, 07:53 AM
why is that?

lior6543
January 24th, 2005, 07:56 AM
...and you should know that i'm not accessing the variables directly. i'm using declared functions inside the class.

MrViggy
January 24th, 2005, 11:44 AM
If the key is the same all the time, then you can just sort the array by the key, and do your searching. However, if the key changes during the life of the program (i.e. you need to search by varA in one function, and varB in another), then you might consider setting up multiple arrays (or better yet, maps) that all just point to concrete instances of your class.

Otherwise, you'll have to either (a) continually re-sort the array; or (b) perform linear searches.

Viggy