1

C# beginner here...

I have

int[] numbers = new int[10];

I would like to add 50 numbers to this array. Once the array is full, the first added number will be removed and the new number will be added. I would like to display the last added number on the top of the array. Such as

[5,4,3,2,1...] 

not

 [1,2,3,4,5,...]

How can I achieve this?

Thanks in advance

This is what I have tried

....
dataArray = new int[10];
....
Queue<int> numbers = new Queue<int>();
....

if (numbers.Count == 10)
        {
            numbers.Dequeue();
        }

        numbers.Enqueue(i);
        numbers.CopyTo(dataArray, numbers.Count);

I keep getting " Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection" error

sherryclln
  • 13
  • 4
  • Have a look at Queue. It won't automatically dequeue items for you, but it gets you a lot of the way there. This question has you answered: http://stackoverflow.com/questions/5852863/fixed-size-queue-which-automatically-dequeues-old-values-upon-new-enques – Andrew Barrett Dec 05 '14 at 03:34
  • 2
    Welcome to Stackoverflow. Show us some code displaying what you have tried. Also, may want to read on [How do I ask a good question](http://stackoverflow.com/help/how-to-ask). – tokyovariable Dec 05 '14 at 03:35
  • I tried Queue and Push methods but I was unable to limit them at 10. They continue adding numbers and my application crashes – sherryclln Dec 05 '14 at 03:36
  • Try creating your own implementation deriving from Queue or List and then do operations based on your need. – Piyush Parashar Dec 05 '14 at 03:37
  • What you are trying to do is not first in first out, it's first in last out – deepmindz Dec 05 '14 at 03:51
  • it is first in first out, aka FIFO. http://upload.wikimedia.org/wikipedia/commons/5/52/Data_Queue.svg – sherryclln Dec 05 '14 at 04:05
  • @sherryclln When speaking of data structures the "X" portion in "First in X Out" stands for "The item that is returned when asking for the next item" – Scott Chamberlain Dec 05 '14 at 04:07

3 Answers3

1

Array.Copy provides good performance for what you're trying to accomplish.

int[] newArray = new int[10];
newArray[0] = 4;    // new value
Array.Copy(numbers, 0, newArray, 1, numbers.Length - 1);

Be careful with array lengths though with this as any bound issues with throw exceptions.

Ashley Lee
  • 3,810
  • 1
  • 18
  • 26
0

Take a look at the generic Queue class, that has the functionality that you are looking for with FILO structure. Doing this just with arrays...

    int[] numbers=new int[50];

    for(int i=0; i<numbers.length; i++) numbers[i]=i; 
    //fill the array with 1, 2, 3, 4...

    //To reverse this order, swap elements in reverse
    //numbers.length-1 is the last index of the array
    // j<numbers.length/2,each time you swap you change two values, so only done half the length of the array
    for(int j=numbers.length-1; j>numbers.length/2; j--) swap(numbers, j, numbers.length-j);
      //swaps the values of different indexes in the array
     void swap(int[] array, int index1, int index2) {
          int temp=array[index1];
          array[index1}=array[index2];
          array[index2}=temp;
                  }

This should reverse the arrays

deepmindz
  • 598
  • 1
  • 6
  • 14
0

I think you should be using queue and then you can convert it into array if you want

class Program
    {
        static void Main(string[] args)
        {
            const int CAPACITY = 10;
            Queue<int> queue = new Queue<int>(CAPACITY);
            for (int i = 0; i < 50; i++)
            {
                if (queue.Count == CAPACITY)
                    queue.Dequeue();
                queue.Enqueue(i);
            }
            queue.ToArray();
            Console.WriteLine(queue.Count);
            Console.ReadKey();
        }

    }
Mahesh Malpani
  • 1,782
  • 16
  • 27