Deletion of UE4 array elements

**

How to delete array elements in UE4

**

Background: In application development, we define an array of type int or string. For example, int a【6】={3, 4, 4, 5, 6, 8}. We're going to delete all 4 of them.

The blueprint nodes available in UE4 are remove item or remove Index

The initial solution: We directly use the for each loop to judge whether the current item is equal to the filtered item, and if so, call the array to directly delete the current Item.

Problem encountered: We found that according to our example, only the first 4 can be deleted. The second 4 cannot be removed.

Reason analysis: When we are making a for each loop judgment, if the current Item is directly deleted, the current array will change. The original 6 elements directly became 5. Join the second element (the first 4) that has been traversed before, and delete the 4. The next time should be to traverse the third element. At this time, it corresponds to 5. The second 4 is ignored directly.

Solution: We can traverse the array first, store all the INdex of Items that need to be deleted, and form a new int type array B. Then process each element of this array B, current Item - current Index, to form a new array C.

Using this array C combined with the original array a, during the traversal process, all specific elements can be removed with the command remove index.

Guess you like

Origin blog.csdn.net/gaofei7439/article/details/117512495