locked
C# pop / push in arrays

    Question

  •  

    Hi, I'm pretty new to C# and am looking for the equalivant I have used in other languages for .pop and push.

     

    I have an array of characters that I have created using .Split on a string. What I want to do is remove the [0] element from that array. In other languages I would just do myarray.pop() but I notice that it isn't available in C# for arrays but only for stacks? Is that correct? could someone please point me to an alternative to using pop that will acheve the same results...or maybe tell me how I could split a string into a stack instead so  I could use .pop?

     

    Thanks!

     

     

    Monday, December 10, 2007 11:30 PM

Answers

  • Code Block

    using System;

    using System.Collections.Generic;

     

    namespace ConsoleApplication1

    {

    class MSDNSample

    {

    static void Main()

    {

    string input = "a b c d";

     

    Stack<string> myStack = new Stack<string>(

    input.Split(new string[] { " " }, StringSplitOptions.None));

     

    // Remove the top element (will be d!)

    myStack.Pop();

     

    Queue<string> myQueue = new Queue<string>(

    input.Split(new string[] { " " }, StringSplitOptions.None));

     

    // Remove the first element (will be a!)

    myQueue.Dequeue();

    }

    }

    }

     

     

    --

    Regards,

    Daniel Kuppitz

    Monday, December 10, 2007 11:46 PM

All replies

  • Code Block

    using System;

    using System.Collections.Generic;

     

    namespace ConsoleApplication1

    {

    class MSDNSample

    {

    static void Main()

    {

    string input = "a b c d";

     

    Stack<string> myStack = new Stack<string>(

    input.Split(new string[] { " " }, StringSplitOptions.None));

     

    // Remove the top element (will be d!)

    myStack.Pop();

     

    Queue<string> myQueue = new Queue<string>(

    input.Split(new string[] { " " }, StringSplitOptions.None));

     

    // Remove the first element (will be a!)

    myQueue.Dequeue();

    }

    }

    }

     

     

    --

    Regards,

    Daniel Kuppitz

    Monday, December 10, 2007 11:46 PM
  • Thanks much Daniel! Thats perfect!

     

     

    Monday, December 10, 2007 11:56 PM