Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing one items from an array and moving the others backwards

Tags:

arrays

c#

.net

I'll just go straight to the point. I want to move the items in an array in a uniform difference, let's say I have this.

string[] fruits = { "Banana", "Apple", "Watermelon", "Pear", "Mango" };

For example, let's say I want to remove the "Apple" so I'll do this.

fruits[1] = "";

Now all that left are:

{ "Banana", "", "Watermelon", "Pear", "Mango" }

How do I really remove the Apple part and get only:

{ "Banana", "Watermelon", "Pear", "Mango" }

Note that the index of all the items from "Watermelon" until the end of the array moves 1 backward. Any ideas?

like image 383
Naufal Fikri Avatar asked Dec 02 '22 01:12

Naufal Fikri


1 Answers

The List class is the right one for you. It provides a method Remove which automatically moves the following elements backwards.

like image 185
Yogu Avatar answered Dec 04 '22 08:12

Yogu