Store "N" number of records in a fixed-size array

Is it possible to store a moving set of values ​​in an array without exceeding the set index?

For example, I put together an example of the base console as follows:

static void Main(string[] args) { int[] myvalue; myvalue = new int[10]; for (int i = 0; i < 20; i++) { Console.WriteLine("Array {0} is {1}", i, myvalue[i]); } Console.ReadLine(); } 

In the current approach, I get an exception from outside. What I would like to do is limit the array to 10 elements, but as the counter increases, overwrite existing elements with new values. Therefore, I could iterate through 100 times, but the array will only show the last ten values ​​(89-99) at the end of the procedure.

If the array is not the best approach, I would welcome any suggestions on how to store this in memory.

Thank you

+4
source share
6 answers

You can use the List to store values, then wrap the Add method to handle the item limit. This will take care of all the slide-down logic.

 const int MAX_ITEMS = 10; List<int> list = new List<int>(); void AddItem(int k) { list.Add(k); if (list.Count > MAX_ITEMS) { // discard the item at the front of the list list.RemoveAt(0); } } 

Whenever you want to add a value, you simply call AddItem(n) , for example:

 for (int i = 0; i < 100; i++) { AddItem(i); } 

Whenever you want to read the values, you can simply do:

 for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); } 

This also has the advantage that you can fit less than the maximum number of elements (let's say you only have 6 instead of 10), and it will still work correctly (you don't have to worry about unfilled elements of the array).

+6
source

Sounds like a circular buffer to me. There is an existing implementation in C # on codeplex.

+6
source

If you want the array to always hold the last ten values ​​in the order in which they occur, you must "shift the values ​​down" to the array each time, copying array[N] to array[N-1] for all N> = 1 and then writing the "last" value in the last position.

If you do not need an array to have values ​​in this particular order, you can use the modulo trick and write to array[i % ARRAYSIZE] for any i ; it will loop until the beginning of the array as soon as you reach the end of the array.

Finally, if you want the values ​​to be in order, and you need a data structure that does not force you to copy all the elements at each iteration, you can use:

  • a linked list if indexed access is optional.
  • two-way queue (aka deque), if you also need indexed access - .NET does not come with built-in, but there is a proven implementation as part of PowerCollections
+3
source

Instead of an array, you can use a circular linked list.

In the snippet below currentNode, the last node is always entered. Thus, when you go around in a circle following a linked list, you can make your list arbitrarily long and never worry about where the end is because at some point you automatically start overwriting the old values.

 static void Main(string[] args) { //create first node var firstNode = new Node(); var lastNode = firstNode; //create linked list for(int i=0; i<9; i++) { var nextNode = new Node(); lastNode.nextNode = nextNode; lastNode = nextNode; } //link tail of list to beginning to make the circular reference. lastNode.nextNode = firstNode; var currentNode = firstNode; //your code goes here for(int j=0; j<1000; j++) { currentNode.value = j; //now you set your values in your loop currentNode = currentNode.nextNode; //and move on to the next node } } public class Node { public int value; public Node nextNode; } 
+1
source

You can try the following:

 myvalue = new int[10]; for (int i = 0; i < 100; i++) myvalue[i % myvalue.Length] = i; 

Of course, this will start from the very beginning when the end is reached, so the array may not be in the order in which you wrote your values, but turned a little. If you want to avoid having to copy items from myvalue[1] to myvalue[myvalue.Length-1] to myvalue[1] to myvalue[myvalue.Length-2] and overwrite myvalue[myvalue.Length-1] every time you insert a new element. Or you can rotate the array to lock the position after all the inserts have been completed.

0
source

This may not be the most elegant code, but it illustrates how to create a circular buffer that writes n the most recent values:

 const int SIZE = 10; int[] myValue = new int[SIZE]; int start = 0; int count = 0; void NewValue(int v) { myValue[start] = v; if (count < SIZE) count++; start = (++start) % SIZE; } void ListValues() { for (int i = start - 1; i >= 0; i--) Console.WriteLine(myValue[i]); if (count >= SIZE) for (int i = SIZE - 1; i >= start; i--) Console.WriteLine(myValue[i]); Console.WriteLine(); } private void Test() { for (int i = 0; i < 20; i++) { NewValue(i); ListValues(); } } 
0
source

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


All Articles