Question If ArrayList contains string remove it from new ArrayList

GoodJuJu

Member
Joined
Apr 26, 2010
Messages
24
Location
England
Programming Experience
5-10
Hi there,

I wonder if somebody could please help....

I have an arraylist (arrFirstArry) which I use to populate another arraylist (arrNoDuplicates) preventing any duplicates from being added. This works fine.

What I would also like my code to do is prevent adding any partial values which may exist from being added.

i.e. my array is like this

'arrFirstArray(0) = "abc"
'arrFirstArray(1) = "abc-"
'arrFirstArray(2) = "abc-123"
'arrFirstArray(3) = "abc"
'arrFirstArray(4) = "abc-"

and I want to end up with

'arrNoDuplicates(0) = "abc-123"

but I end up with

'arrNoDuplicates(0) = "abc"
'arrNoDuplicates(1) = "abc-"
'arrNoDuplicates(2) = "abc-123"

VB.NET:
            Dim arrFirstArray As New ArrayList()
            
            'arrFirstArray contains say.....
            'arrFirstArray(0) = "abc"
            'arrFirstArray(1) = "abc-"
            'arrFirstArray(2) = "abc-123"
            'arrFirstArray(3) = "abc"
            'arrFirstArray(4) = "abc-"
            
            arrFirstArray.Sort()
            arrFirstArray.Reverse()
            
            Dim arrNoDuplicates As New ArrayList()
            
            For nd As Integer = 0 To arrFirstArray.Count - 1
                If Not arrNoDuplicates.Contains(arrFirstArray(nd).Trim()) Then
                    arrNoDuplicates.Add(arrFirstArray(nd).Trim())
                End If
            Next

What I would like to end up with in my new array (arrNoDuplicates) is arrNoDuplicates(0)="abc-123"

I don't see how it does not work as if I reverse the first array at the beginning, it should put arrNoDuplicates(0)="abc-123" as the first entry and then the next entry it tries to place should not be added as 'abc-123' ".contains" 'abc-'.

Any help anybody can offer is much appreciated, I hope I have explained myself clearly.
 
Last edited:
You are misunderstanding arrNoDuplicates.Contains

The Contains function works a little bit differently depending on what datatype you are working with.

For ArrayLists, the Contains function will iterate over each item in the ArrayList searching for whatever you have as the parameter. When it does this search it does a comparison by the '=' operator. This means the item you are searching for must match exactly to the item in the ArrayList.

The Contains function does something similar to this:
VB.NET:
Public Function Contains(ByVal strSearchItem as string) as Boolean
  for each strListItem as String in MyArrayList
     if strSearchItem = strListItem Then
        return True
     end if
  end For

   return false
end Function

What you are looking to do is iterate over each item in the ArrayList and see if any of the items Contains the searchItem. Which would be something like this...

VB.NET:
Public Function Contains(ByVal strSearchItem as string) as Boolean
  for each strListItem as String in MyArrayList
     if strListItem.Contains(strSearchItem) Then
        return True
     end if
  end For

   return false
end Function

Now instead of checking each item in the ArrayList to see if any items match the SearchItem EXACTLY it will check each item to see if any items CONTAIN the SearchItem.
 
rcombs4,

Thanks for your reply. I wondered if that might be the case as it was stopping the exact matches being added to my new arraylist. I dismissed that thought as I was also using 'contains' on a string search which was working fine. Like you say - "The Contains function works a little bit differently depending on what datatype you are working with".

I now see the logic of what I need to do, I am just struggling to implement it. I tried manipulating the arraylist, but when I go to next value, I get the error that the values have been modified.
 
Back
Top