- Home /
Remove element from array
hi dudes!
I'm trying to remove an element from an array, though the array I'm using has a different syntax to the one i can find that I found in the manual.
how can i remove an element from this?
Color[] colors = {Color.green,Color.red, Color.white, Color.blue, Color.yellow, Color.black};
Arrays have predefined size so you cannot simply add or remove slots. The easiest way is to find an index of your element that you want to remove and then assign null value to it. Like this:
int index = 0; // any nuber you need
colors[index] = null; // removes an element from the "index slot"
Since Color is a value type you can't set it to "null". Even when it was a reference type (or nullable type) the element is not "removed" since it's still there but with the value of null.
Answer by Bunny83 · Sep 30, 2015 at 11:29 AM
Like others have already mentioned, arrays have a fix size. Once created the size can't be "changed". However it's possible to "resize" an array, but it actually creates a new array and copies all values over.
See this SO answer:
public static void RemoveAt<T>(ref T[] arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
// moving elements downwards, to fill the gap at [index]
arr[a] = arr[a + 1];
}
// finally, let's decrement Array's size by one
Array.Resize(ref arr, arr.Length - 1);
}
RemoveAt(ref colors, 2); // removes Color.white.
Using a generic List is recommended since it only creates a new array if necessary. So if you add / remove a lot elements a List will be better in speed and "garbage production".
Thanks guys! I'm gonna come back to this and try and get my head round it so don't think that ive forgotten, I'm thinking i should tidy up some of my code before i get to it :)
If the order of the array does not matter you can also do just:
public static void RemoveAt<T>(ref T[] arr, int index)
{
// replace the element at index with the last element
arr[index] = arr[arr.Length - 1];
// finally, let's decrement Array's size by one
Array.Resize(ref arr, arr.Length - 1);
}
Array does not exist in the current context :v
That is the error Unity show me with this code
Answer by Denvery · Sep 30, 2015 at 11:06 AM
You can't remove the element from this array in lite, normal, intuitive ways. Please use List<Color>
for enabling removing tool
https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
Answer by ravenclarico · Jun 21, 2021 at 08:35 PM
Most people will tell you to use Lists instead, however that's a cop-out and doesnt really answer the question (but they're probably right) if you really need to remove an element from an array, and you can't use lists or any other libraries, I'm pretty sure the only way is to create a new array and replace the old one with it. below is an example of a function that you could use to do that
int[] removeAtIndex(int[] inputArray, int index){
int[] outputArray= new int[inputArray.length-1];
for(int i=0;i<index;i++){
outputArray[i]=inputArray[i];
}
int writeLoc=index;
int readLoc=index+1;
for(int writeLoc=index;writeLoc<outputArray.Length;writeLoc++){
outputArray[writeLoc]=inputArray[readLoc];
readloc++;
}
return outputArray;
}
Your answer
Follow this Question
Related Questions
array question 2 Answers
how to delet used array item 2 Answers
How do i get my script to work? 2 Answers
C# array equal to another array minus one entry. 1 Answer
how to add list after removing some 1 Answer