Efficient algorithms to shift items in an arraylist to the left

Posted on 2006-03-28
Last Modified: 2012-08-13

Let's say that I have an arraylist that has a length of 20.

I am adding 20 items to the list, and it fills up.

Upon adding the 21 item, I would like to discard the item at index 0, and shift all the items over index - 1, and then add the 21st item to the end of the list, and basically maintain this logic for each new item added.

I know that perhaps a linked list would be good for this type of collection, but do you guys have any ideas for how to implement using an arraylist? The reason is because I am using the List object exposed by CollectionBase... and I would like to easily bind this custom collection as a datasource.

Question by:gregasm
    LVL 8

    Author Comment

    I just threw something together that looks like this:

                public int Add( ThruputData data )
                      if ( List.Count >= MAX_DATAPOINTS )
                            List.RemoveAt( 0 ) ;
                      List.Add( data ) ;
                      return List.IndexOf( data ) ;

    What would you do?
    LVL 21

    Accepted Solution

    so, what's wrong with your approach - you're doing it in the linked list fashion, it will add an item at the end of your array and remove one at the front when it reaches the pick - do you still need any help on this one?

    LVL 10

    Assisted Solution

    I can't see anything wrong with the solution you've found yourself, it was the first thing I thought of when I read your original question, before I scrolled down the page & saw your solution.

    Give yourself the points 8)
    LVL 8

    Author Comment

    Well, I started off with the datatable approach since it is the first object I think of when databinding. But my datatable solution would be to implement sorting on the table in hidden column, say by timestamp, but this seemed like the approach was too heavy for what I was looking for.. a lightweight implementation.

    That's how I got into the custom databinding, and was just wondering what you guys would have done.

    Thanks for participating.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now