Add to list but can never remove

GW_

New member
Joined
Jul 5, 2017
Messages
5
'TEST
Local L:List<float[] > = New List<Float[] > ()
L.AddFirst(New Float[2])
Local fa:Float[] = L.First()
L.Remove(fa)


Error: Arrays cannot be compared
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
681
This seems to be kind of a missing functionality due to the fact that it is ambiguous under which cirumstances arrays are equal. Do you need the reference to be equal or the content?

What do you want to achieve? Do you only need it to remove the first array or do you want to remove an arbitrary one?
 

GW_

New member
Joined
Jul 5, 2017
Messages
5
I don't understand your questions. The title of the post and the example explain everything.

Do you need the reference to be equal or the content?
I need to remove an item from the list.

What do you want to achieve?
Remove an item from the list.

Do you only need it to remove the first array or do you want to remove an arbitrary one?
It doesn't matter. An item cannot be removed from the list once it's added.
 

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,281
Yes, items can be removed. The problems are Arrays and the way you want them to be removed. Arrays can currently only be removed via their ListNode which will be returned when you add an item to a list and you need to store that Node somewhere. Then it is simply a call

Cerberus:
yourNode.Remove()

That is by far the fastest way to remove a node from a list.

The call of List.Remove(item) loops through the whole list and trys to find ALL items that are equal and deletes them. The problem with arrays is that a comparison for ARRAYS is currently not implemented. We have an idea how to do it, but it might not work anyway.
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
681
I need to remove an item from the list.
If you don't mind the content to be compared it might be better using a List of Lists instead of a List of arrays.

For your special example something like this works:
Code:
Strict

Function Main:Int()

    Local L:List<Float[]> = New List<Float[]>()
    Local arr1:Float[]=[1.0,2.0]
    Local arr2:Float[]=[3.0,4.0]
    Local arr3:Float[]=[1.0,2.0]
    
    Print L.Count() + " Arrays left."
    
    L.AddFirst(arr1)
    L.AddFirst(arr2)
    L.AddFirst(arr3)
    Print L.Count() + " Arrays left."
    
    Local foundNode:list.Node<Float[]>
    foundNode = L.FirstNode()
    If foundNode foundNode.Remove
    foundNode = L.FirstNode()
    If foundNode foundNode.Remove
    Print L.Count() + " Arrays left."

    Return 0
End
 

Gerry Quinn

Active member
Tutorial Author
Joined
Jun 24, 2017
Messages
163
Another way would be to wrap the array in a class and have a comparison for the class.
 
Top Bottom