-2

So I've got a Datapoint[] = new Datapoint[50] that I'm filling. As soon as the Datapoint array is full, how can I "delete" Datapoint[0] and set Datapoint[1] to Datapoint[0], Datapoint[2] to Datapoint[1]... etc. and make Datapoint[49] free again / to value 0/0?
I didn't had any success yet figuring it out since I don't wanna use a loop.

Thanks for help!

C.User
  • 153
  • 18
  • 1
    What about using a list? or a linked list? As for using the array, simply write a for-loop? `for (int i = 0; i < 49; i++) Datapoint[i] = Datapoint[i + 1]; Datapoint[49] = 0;` – Lasse V. Karlsen May 07 '18 at 13:46
  • @mjwills yes it solved my problem! add it as answer and i'm going to mark it as answer and upvote it! thanks sir. – C.User May 07 '18 at 13:47
  • 3
    If you do that operation a lot then an array is not the right data structure to use. – Eric Lippert May 07 '18 at 13:56
  • 1
    You say that you haven't figured it out. What have you tried so far? (Hint: doing a repeated operation is usually done with a *loop*.) – Eric Lippert May 07 '18 at 13:56

1 Answers1

5

One option to consider would be to create a completely new array and assign it to your existing variable.

The code would look something like:

existingVariableName = existingVariableName
    .Skip(1)
    .Concat(new Datapoint[] { new Datapoint() })
    .ToArray();

Skip(1) means to skip over the existing first element. Concat is used to add a new element at the end. ToArray materialises the LINQ query into the new resulting array.

mjwills
  • 23,389
  • 6
  • 40
  • 63