Efficient algorithms to shift items in an arraylist to the left


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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gregasmAuthor Commented:
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?
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?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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)
gregasmAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.