473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ArrayList or ?

I need to keep track of up to 100 values. When the list reaches 100, I'd
like the first to be removed and the new item added to the end. My first
thought was the Stack class, but I need to be able to update items 0-99
with updated values. The version I ended up with today is an ArrayList,
where I do...

If (myList.Count = 100) Then myList.RemoveAt(0)
myList.Add(newItem)

Does anyone have any suggestions for me? I'm not sure how expensive that
RemoveAt is.

Thanks!
Jason.
Nov 20 '05 #1
2 1030
"Jason Reis" <jr*****@hotmail.com> wrote...
I need to keep track of up to 100 values. When the list reaches 100, I'd
like the first to be removed and the new item added to the end. My first
thought was the Stack class, but I need to be able to update items 0-99
with updated values.


Hi Jason,

You are actually describing a queue not a stack... you might take a look at
system.collections.queue and see if you can use that.

Tom
Nov 20 '05 #2
In article <19*****************************@40tude.net>, Jason Reis wrote:
I need to keep track of up to 100 values. When the list reaches 100, I'd
like the first to be removed and the new item added to the end. My first
thought was the Stack class, but I need to be able to update items 0-99
with updated values. The version I ended up with today is an ArrayList,
where I do...

If (myList.Count = 100) Then myList.RemoveAt(0)
myList.Add(newItem)

Does anyone have any suggestions for me? I'm not sure how expensive that
RemoveAt is.

Thanks!
Jason.


Jason,

Since you know that the queue is really going to be a fixed size
structure, I would suggest you implement your own queue class using an
array of items as the backing store. You could implement it as a
circular queue. Here is a very simple demonstration. You may want to
implement this as full typed collection, etc - but I just threw this
together to sort of demonstrate the algorithm... I believe I actually
learned this from an old VB algorithm book by Rod Stephens:) I'm sure
that there maybe logical errors in this - but, the basic principal is
sound :)

Option Explicit On
Option Strict On

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(108, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ListBox1
'
Me.ListBox1.IntegralHeight = False
Me.ListBox1.Location = New System.Drawing.Point(4, 40)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(288, 232)
Me.ListBox1.TabIndex = 1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim iq As New ItemQueue
Me.ListBox1.Items.Clear()
For i As Integer = 1 To 200
iq.Enqueue(New Item(i))
Next

For i As Integer = 1 To 100
Me.ListBox1.Items.Add(iq.Dequeue)
Next
End Sub

Private Class Item
Private _id As Integer

Public Sub New(ByVal id As Integer)
Me._id = id
End Sub

Public ReadOnly Property ID() As Integer
Get
Return Me._id
End Get
End Property

Public Overrides Function ToString() As String
Return _id.ToString()
End Function
End Class

Private Class ItemQueue
Private _items(99) As Item
Private _front As Integer
Private _back As Integer

Public Sub Enqueue(ByVal value As Item)
_items(_back) = value
_back = (_back + 1) Mod _items.Length
End Sub

Public Function Dequeue() As Item
Dim value As Item
value = _items(_front)
_items(_front) = Nothing
_front = (_front + 1) Mod _items.Length
Return value
End Function

End Class
End Class

Anyway, for your simple case (100 elements), I don't think your going to
gain a lot in performance. But with larger numbers of elements, an
array approach would definately be more efficient.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
6
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow =...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.