 |
 |

04-20-2006, 06:56 PM
|
 |
Centurion
|
|
Join Date: Mar 2005
Posts: 167
|
|
Is this bad programming or am I just a Genius :p
|
I have a while loop that incraments an arraylist which holds index locations to a second array, however during this while loop an element may be removed from a second arralist and the program will crash as the element is not longer there, so I have used an exception handler which then removes this element from the arraylist when it finds the element in the second arraylist is no longer there, thus allowing the while loop to skip it and then continue to the next element in the first arraylist.
Now is this bad programming practise by letting it generate an exception on purpose because I don't see anyway to get it to check to see if that element still exists. Man I wish I had used a hashtable but it's too late for that now
is there a way to check to see if an object exists in a hashtable by supplying it with a key.
|
|

04-20-2006, 07:25 PM
|
 |
Hopelessly confused...
* Expert *
|
|
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,054
|
|
|
can you not usa a for each loop and only check the ones that do exist, or am I missing something?
|
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)
For the love of Gold...
|

04-20-2006, 08:28 PM
|
|
Ultimate Contributor
Retired Leader * Expert *
|
|
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
|
|
You can use a combination of ArrayList.Contains and ArrayList.Count to determine if an ArrayList contains a specified object or if a specific index is out of range.
The Hashtable provides a ContainsKey method that's pretty self-explanatory
You should NEVER use exceptions for controlling application flow - it gets too expensive and it violates the idea of an exception. Exceptions should only occur in truly exceptional circumstances.
|
|

04-21-2006, 08:07 AM
|
 |
Keeper of foo
Retired Moderator * Guru *
|
|
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
|
|
|
In other words, not a genius.
|
__________________
~ Quod non mortiferum, fortiorem me facit ~
Avatar by lebb
|

04-21-2006, 08:53 AM
|
 |
Centurion
|
|
Join Date: Mar 2005
Posts: 167
|
|
|
I can't use a for loop as it doesn't like you deleting elements as it messes up the iteration. And I don't think the contains in the arraylist will work since the numbers change since they are index positions. Since what used to be 2 is now object 3, with a hastable it doesn't change.
Ok ok lol I didn't mean look at me I know something you don't, have you not ever thought of something and then thought I am quite proud for thinking that?
|
|

04-21-2006, 09:03 AM
|
 |
Senior Contributor
|
|
Join Date: May 2005
Location: Manchester,England
Posts: 1,293
|
|
when I was first learning programming then yes. not so much these days 
|
|

04-21-2006, 09:15 AM
|
|
Ultimate Contributor
Retired Leader * Expert *
|
|
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
|
|
|
If you're using a For loop and want to delete elements, step backwards through the collection/array...
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|