Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct form to Slice an Array In C#

Tags:

arrays

c#

i wanted to ask how to Slice an array without using Array.Copy. I'll give you an example of what i want to achieve so you can understand me.

Let's say i have this Array. called Original

[ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 ]

i want to get a copy array from an starting index given some length, lets say i want element one to element 6 i want the code to perform the task such as

int startIndex = 0;
int lenght= 5;
int[] CopyArray = ArrayFromRange(Original, startIndex, length);

then copyArray would be:

[ 1 | 2 | 3 | 4 | 5 ] i do not want to use Array.Copy because i will loop this to get subsequent slices

so i would do

int length = 3;
for(int i = 0; i < OriginalArray.Length; i++)
{
     int[] CopyArray = ArrayFromRange(OriginalArray, i, length);
     // perform some operations
}

This would give me an array of 4 elements every time the loop executes it and then I'd do some operations. But if I did Array.Copy it would throw an OutOfBoundsException when the i in the loop gets the value 13 it would try to copy the array[15] which does not exists. I want to avoid these kind of errors.

I'm developing on Winforms, .Net 4.0

like image 393
HardCodeStuds Avatar asked Jul 06 '13 01:07

HardCodeStuds


People also ask

Can you slice an array in C?

Array-slicing is supported in the print and display commands for C, C++, and Fortran. Expression that should evaluate to an array or pointer type. First element to be printed. Defaults to 0.

How do you slice an array?

Array.prototype.slice() The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array. The original array will not be modified.

Can you slice an array?

Definition and Usage. The slice() method returns selected elements in an array, as a new array. The slice() method selects from a given start, up to a (not inclusive) given end. The slice() method does not change the original array.

Can we do slicing in C?

Slicing Strings with strsep()The strtok() function is the traditional C library routine used to slice up a string. It has its limitations, however, so a more recent function was included in the library on some compilers to replace it: strsep().


2 Answers

I think the best way to treat your approach would be with IEnumerable objects, specifically using LINQ. IEnumerable is an interface meaning "you can call foreach on this object". It's implemented for arrays, and also for some other objects used to query collections - part of the point being, you don't especially need to know WHAT those objects are; just enumerate each item when you need them.

using System.Linq; // put this at the top of the .cs, not mid-code.

int startIndex = 0;
int lenght= 5;
IEnumerable<int> CopyArray = Original.Skip(startIndex).Take(lenght);

I wrote that in notepad, so it's possible there's some small thing I missed, but that should give you what you need. You can skip the .Skip(startIndex) part if it's always going to be 0. To access each int:

foreach (int value in CopyArray) {
   // TODO with value??
}
like image 90
Katana314 Avatar answered Sep 19 '22 12:09

Katana314


You can still use Array.Copy() but you just need to make sure that the index is within range:

T[] ArrayFromRange<T>(T[] originalArray, int startIndex, int length)
{
  int actualLength = Math.Min(length, originalArray.Length - startIndex);
  T[] copy = new T[actualLength];
  Array.Copy(originalArray, startIndex, copy, 0, actualLength);
  return copy;
}

In your example, the last two arrays in the loop would be {14,15} and {15}.

like image 22
Mark Cidade Avatar answered Sep 17 '22 12:09

Mark Cidade