Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
i have a cartesian point. i want to insert it into an arrayList of cartesian points. before i insert it, i want to make sure it doesnt already exist. how would you go about this?
Posted

The easy way would be to search the list until you find the point then return true, meaning you found it.

 

to psuedo it out...

 

bool Exists(newPoint)
{
  for each point in someList
  {
     if point == newPoint
     {
        return true  //found the point, no need to keep looking, don't add it.
     }
  }
  return false  //searched entire list, didn't find it, add it.
}

 

I believe there might also be an exists method in arrayList (I could be wrong) but I think that will only compare memory locations, not wether the two points are, value-wise, equal...it will only compare the references or pointers, not the values, or dereferenced pointers. That's probably more confusing than it needs to be....

  • *Experts*
Posted

Not knowing everything about the ArrayList structure you might also try:

 

Dim sample As New ArrayList()

If sample.Contains('blah) Then

'some action

Else

'some other action

End If

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

contains, that's what I was thinking. Not exists.

 

Won't that only compare the references and not the values of the objects, though? Unless the objects are primitives? Except they aren't called primitives in .Net, their called...base types? (string, intiger, float, etc...)

  • *Experts*
Posted

That's why I prefaced my post with "Not knowing everything about the ArrayList structure"

 

If the ArrayList stores a list of points in the {X, Y} format then comparing a new point in the same format with .Contains works fine. Otherwise your method is required.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...