Removing Elements From Arrays in SpriteKit/Swift - arrays

When you remove a SpriteNode from Parent in SpriteKit, does it automatically remove it from any arrays that it is in? Also, when you remove an element from an array, does every other element shift or is there a gap where the element used to be?

Yes when a node is removed from a parent it is removed from the parent in every way. There is no array automatically created for a parent and its children. If you create an array manually you'd have to remove the element manually as well. When an element is removed it doesn't leave a gap in between. All the elements after it just shift one spot down.
var array = [0,1,2,3] //A new array
array.removeAtIndex(1) //The 1 would remove the element in the second place which is a 1 in this case
//Now the array should look like [0,2,3] instead of [0,,2,3]//The one is not nil. No gap in between.

Related

Implement a stack using the first element of an array as top

I am implementing a stack in C. The task i have been given requires me to use the first element of the array as top.
For example:
If i have the struct:
struct stack {
int arr[MAX];
int top
};
I need to assign the first element of arr(i.e. arr[0]) to top and then implement the stack.
I don't get the question as top is usually assigned the value -1 and is accordingly incremented or decremented. What exactly do I need to do here?
This is a rather strange requirement, because the most natural way to implement the stack is using the last element of the array as the top by adding new elements at the end of the array and removing them from the end.
This way you will satisfy the LIFO requirement of the stack and also avoid moving the rest of the data in the array as you would have if you were adding and removing elements from the beginning of the array.
Also I'm not sure what is the top member of your struct is supposed to do, but the most natural thing to have here is some way to indicate what is the last element of the array currently is (i.e. the top of the stack). So it seems to me that the top member should contain the index of the last element in the array and by doing stack.arr[stack.top] you will retrieve the top element of the stack.

Removing a specific element from an array by name in AS3

I would like to remove a specific element from an array, not by index because the index value of that item is not static.
myarray.splice(myclip, 1);
When I use this code instead flash removes the first element in the array.
Is there something I am missing here?
Documentation of splice()
Both parameters need to be integers, the first one is the position of the element you want to delete, and the second one is the amount of elements you want to delete. Try myarray.splice(myarray.indexOf(myclip),1);
Don't know why it would only remove the first element in your snippet, maybe internally it casts myclip to 0? Doesn't matter, use indexOf. If that doesn't work, for loop through the array to get the position first.

How can I remove an element from an array at an index position without permanently changing the array?

How do I remove an item from an array at a specific index without permanently removing the item from the array? I have
my_data_col.tap{|i| i.delete_at(index)}.select{|item| ... }
I notice that, after the call, my_data contains one element fewer than it did when I started. How do I remove the element at the index without permanently altering the array?
Given your example in #Ursus's post (now deleted) I would execute as follows:
my_data_col.select.with_index do |str,idx|
str.respond_to?(:downcase) && idx != index
end.map(&:downcase).uniq
Next time please post your full example so people can assist more easily

Ember.Array: how can I make several changes atomically?

I have an Ember.Array that I need to rearrange by moving one element to another. Right now, I have the function:
moveElement: (from, to) ->
from_value = array.objectAt(from)
array.removeAt(from)
array.insertAt(to, from_value)
The problem is, I use this array in a computed property and that is firing in between the removeAt and insertAt. How can I make sure these changes are atomic?
Thanks!
So, I managed to get it working by using arrayContentWillChange, arrayContentDidChange, and array primitives:
moveElement: (from, to) ->
array = #get('array')
# make all changes to array atomically
array.arrayContentWillChange(from, 1, 0)
from_value = array[from]
array.splice(from,1)
array.splice(to,0,from_value)
array.arrayContentDidChange(to, 0, 1)
I'm not sure if I'm using the arrayContent..Change functions correctly. They take 3 arguments: startIdx, removeAmt, addAmt. Which represent the start index, number of elements to remove, and number of elements to add. I used my from and to values to show that I am removing one element at from and adding one element at to.
Seems to be working now.

Remove a term from an array during a for loop

I've been creating a system for storing objects that persist for a specified duration, then remove themselves. However, I'm having some trouble figuring out how to remove expired items from the array while the for loop is running (to minimize extra iterations through the array)
Here is what I've made so far. Terms in the list are flagged with a Boolean value indicating that they're done.
For i = 0 To VisualEffects.Count - 1 Step 1
VisualEffects(i).Update(gt)
VisualEffects(i).Draw(sb, Pos, Cam)
If VisualEffects(i).CD.isExpired() Then
VisualEffects.RemoveAt(i)
i = -1
End If
Next
Why does this produce an error? How can I remove a term from an array and continue iterating through the remainder of the loop?
Regards
Ares
The line
i = -1
sets i to -1, causing the For loop to terminate.
If you can, reverse the order of the loop so that you start with the last array element and count down. That makes the remove logic much more straightforward.
.NET arrays are fixed-size so you can't actually remove an element from an array at all. You can change the value of an element, so you can set an element to Nothing to remove the object that it referred to, but the element is still there. If you read the MSDN documentation for the Array class, you'll see that the RemoveAt method throws a NotSupportedException for this reason.
If you're actually using a collection rather than an array then you can call RemoveAt but you need to loop from the last index to the first rather than first to last. That way, removing an item will not affect the indexes of those items that you are yet to visit.

Resources