Removing one element from the array and moving the rest back

I'll just go straight to the point. I want to move elements 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 "Apple", so I will do it.

 fruits[1] = ""; 

Now all that remains is:

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

How can I remove part of Apple and get only:

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

Please note that the index of all elements from "Watermelon" to the end of the array moves 1 back. Any ideas?

+4
source share
7 answers

If you really want to use Arrays, you can use Linq to filter your list and convert to an array:

  string[] fruits = { "Banana", "Apple", "Watermelon", "Pear", "Mango" }; fruits = fruits.Where(f => f != "Apple").ToArray(); 

If you do not need to use an array, check out the List class. The list allows you to add and remove items.

+6
source

The List class is right for you. It provides a Remove method that automatically moves the following items backward.

+9
source

As for the Wouter answer, if you want to remove by the index of the element, not the value of the element, you can do:

 fruits = fruits.Where((s, i) => i != 1).ToArray(); 
+2
source

You can do something like this:

 for( int i = 1; i + 1 < fruits.Length; i++ ) fruits[i] = fruits[i + 1]; fruits = System.Array.Resize( fruits, fruits.Length - 1 ); 

If you don't need the order of the fruits in the array, a more reasonable way to do this is:

 fruits[1] = fruits[fruits.Length - 1]; fruits = System.Array.Resize( fruits, fruits.Length - 1 ); 
+1
source

I think one of the most useful things a new programmer can do is to learn and understand the different types of collections.

Although I think the List option that others have mentioned is probably what you are looking for, it's worth looking at the LinkedList class if you are doing a lot of attachments and deletions, rather than a lot of index searches.

+1
source

This is an example of how I used lists and arrays to remove an element from an array. Note. I will also show you how to use linq to search for an array with bad names for deletion. Hope this helps someone.

 public static void CheckBadNames(ref string[] parts) { string[] BadName = new string[] {"LIFE", "ESTATE" ,"(",")","-","*","AN","LIFETIME","INTREST","MARRIED", "UNMARRIED","MARRIED/UNMARRIED","SINGLE","W/","/W","THE","ET", "ALS","AS", "TENANT" }; List<string> list = new List<string>(BadName); //convert array to list foreach(string part in list) { if (BadName.Any(s => part.ToUpper().Contains(s))) { list.Remove(part); } } parts = list.ToArray(); // convert list back to array } 
0
source

As a newbie 3 years ago, I started creating software that I am still working on. I used the array for the "PartyMembers" game, and today I regret it and spend a ton of time converting all of this hard-coded $ #! T to the list.

In this case, just use lists, if you can, for comparison - this is a nightmare.

0
source

Source: https://habr.com/ru/post/1388291/


All Articles