How to remove ranges from an array in C #

How to remove ranges from an array in C # How with ArrayList ?

 ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); myAL.RemoveRange( 4, 3 ); 

How can I achieve the same with a string array?

+4
source share
2 answers

Shared lists expose the RemoveRange() method. You can convert your array to a list, then delete the range, and then convert back to an array:

 var myList = myArray.ToList(); myList.RemoveRange(index, count); myArray = myList.ToArray(); 

To remove only one item with a specific index, you can use RemoveAt() :

 var myList = myArray.ToList(); myList.RemoveAt(index); myArray = myList.ToArray(); 
+8
source

You will need to iterate over the indices you need, and then copy them to a new array, since arrays cannot be edited this way, so the list of arrays is about it.

0
source

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


All Articles