Computer Science Canada A few ArrayList questions |
| Author: | Reality Check [ Thu May 15, 2008 9:15 am ] |
| Post subject: | A few ArrayList questions |
So I'm making a Friend book type of thing for ArrayList practice and I have a few questions. I made my Friend class and I've worked out adding, viewing, and removing your friends but I have a few other things I want. Firstly, searching. I'd like for them to be able to search using any of the friend attributes (like name, phone number, email). However, I cannot think of an efficient way. I could just search every attribute of every friend added but that doesn't feel right. I also thought of forcing the user to choose what to search (name and stuff) but I'd rather have them type in anything and it searches. Another idea I had for search was doing a quick check as to what their search entails so I know which Friend class variables to check. If their search has an '@' sign then I know it's an email and then I don't need to waste time searching for everything else. If its a 10 digit number then I know it's a phone number and so on. It's a bit better and that's the best way I can think of. Furthermore, I'd like the search to be able to find something even if it isn't an exact match and I have no idea how to do it. One last thing is sorting my Friends Alphabetically with name. |
|
| Author: | wtd [ Thu May 15, 2008 10:00 am ] |
| Post subject: | RE:A few ArrayList questions |
Ultimately the solution to your quest for searching efficiency is to store your data in some form of real database. |
|
| Author: | wtd [ Thu May 15, 2008 10:12 am ] |
| Post subject: | RE:A few ArrayList questions |
But on further consideration of your question, I believe you should investigate the Comparator interface, and anonymous inner classes |
|
| Author: | Reality Check [ Thu May 15, 2008 3:16 pm ] |
| Post subject: | Re: A few ArrayList questions |
Thank you wtd. I'll look up those classes for sure and I guess I'll just save all of my info in a file where it would then be much easier to search. |
|
| Author: | DemonWasp [ Wed Jun 11, 2008 2:56 pm ] |
| Post subject: | RE:A few ArrayList questions |
If you just want a simple String-matching search that will happen to match any field of each Friend, then you could just append all the values of the fields of each Friend together (i.e. name + phoneNumber + email + ...) and do an indexOf on that. Crude, but it would work. Probably actually only slightly less work than just searching to see if their query matches each field. |
|