Android Question Array Shuffle Error B4A and B4J

rodmcm

Active Member
Licensed User
This is cut down program to demonstrate a fault I just cant fathom

The program simply deletes a record (No 17 for an example) in an array of 20 item by shuffling the other array values down and then assigning a default value to the last record. So values for 18 goes to 17, 19 goes to 18, 20 goes to 19

All well and good until I put the last record(20) values to the default 9999.. Both the 19 and 20th record values go to 9999. Same happens if I put the 19 record to 9999..... What is going on please?

The screen shot is of the logs..

This happens on B4A and B4J
 

Attachments

  • Capture.JPG
    Capture.JPG
    38.4 KB · Views: 152
  • Delete_Array_Line.zip
    2.2 KB · Views: 141

stevel05

Expert
Licensed User
Longtime User
It's because you are trying to copy objects, which only copies a pointer to the object, so two array elements end up pointing to the same array.

Your Shuffle code should be :

B4X:
    ' shuffle the records down, only to the second to last record
    For j = TypeSelection To Profile.Length-2
        Profile(j).ItemNo = Profile(j+1).ItemNo
        Profile(j).Cost = Profile(j+1).Cost
    Next
 
Upvote 0

rodmcm

Active Member
Licensed User
Sorry for delay in replying..Thanks for that it worked but I do not understand your comment about two element arrays pointing the the same array.

In my tiny brain each element of the array references a profile(j) that has the object data associated with that array item.. Can you explain further please
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
two array elements end up pointing to the same array
Should have been 'pointing to the same object.'

each element of the array references a profile
Yes the key word is references, not contains.

Initial.jpg
After applying Profile(j) = Profile(j+1)
After.jpg


As the objects are not moved, Element A is no longer referenced and Elements 3 and 4 are both referencing Object E
 
Upvote 0
Top