473,763 Members | 6,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(newI tem)

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

Thanks!
Jason.
Nov 20 '05 #1
2 1042
"Jason Reis" <jr*****@hotmai l.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.collecti ons.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(newI tem)

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.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.ListBox1 = New System.Windows. Forms.ListBox
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(108, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'ListBox1
'
Me.ListBox1.Int egralHeight = False
Me.ListBox1.Loc ation = New System.Drawing. Point(4, 40)
Me.ListBox1.Nam e = "ListBox1"
Me.ListBox1.Siz e = New System.Drawing. Size(288, 232)
Me.ListBox1.Tab Index = 1
'
'Form1
'
Me.AutoScaleBas eSize = 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim iq As New ItemQueue
Me.ListBox1.Ite ms.Clear()
For i As Integer = 1 To 200
iq.Enqueue(New Item(i))
Next

For i As Integer = 1 To 100
Me.ListBox1.Ite ms.Add(iq.Deque ue)
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
455
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 arrayList to the datagrid. private void BindQuestions()
3
1895
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 pItem. As I understand it (and I coded it to test) if I try to use pAttribute again by clearing with pAttribute.Clear because it is a shallow copy the pItem is also cleared. I guess I need a "deep copy" but can't find any documentation on it. ...
3
2857
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 datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
6
5387
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 = dtLCPack.NewRow(); int intColCount = dtLCPack.Columns.Count; ArrayList arrlRow = (ArrayList)alLCPlist; <== Specified cast
4
2819
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 that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
18
4743
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
18
3254
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 storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
6
3140
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 to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
3
2631
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: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
1
3508
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 the ArrayList back from pgm3 how to extract and separate the fields out fo the ArrayLists? Question2: When pgm3 gets ArrayList back from pgm2 how to separate the
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.