Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How then can I access an element-item of a stack through the collection 2

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
GR
Hello everyone, I have created the following subroutine that creates a collection and each element of the collection is a stack. I would like to mention, that each stack element is class object with some properties that I want to access.
How then can I access an element-item of a stack through the collection???? Any suggestions please??? Any help will be much appreciated. Thank you so much in advanced.

Public Sub CreateInitialStacks()
Dim UsrCheckerCollection As System.Collections.ArrayList = New System.Collections.ArrayList
Dim UsrStack As Stack = New Stack
Dim cnt As Integer = 0
For i = 0 To UsrCheckers.Length - 2
If i < UsrCheckers.Length - 2 Then
If UsrCheckers(i).Position = UsrCheckers(i + 1).Position Then
If cnt = 0 Then UsrStack.Push(UsrCheckers(i))
UsrStack.Push(UsrCheckers(i + 1))
cnt = cnt + 1
Else
UsrCheckerCollection.Add(UsrStack)
UsrStack.Clear()
cnt = 0
End If
ElseIf i = UsrCheckers.Length - 2 Then
UsrCheckerCollection.Add(UsrStack)
UsrStack.Clear()
cnt = 0
End If
Next

End Sub
 
Accessing an element-item of a stack is defeating the purpose of a stack; only the most recent element pushed onto the stack should be available to be popped off of the stack. Another data structure would be a better choice here.

Also, since you've only created one instance of a Stack, every time you perform a UsrCheckerCollection.Add(UsrStack), you're adding the same instance to the collection. Clearing or pushing items will affect each element of UsrCheckerCollection.

Next, are you in a position to use classes from the System.Collections.Generic namespace? I think they'll make your code a lot simpler...
Code:
[green]' Assuming UsrCheckers(i) is an Integer ...[/green]
Dim UsrCheckerCollection = New List(Of Stack(Of Integer))
Dim UsrStack = New Stack(Of Integer)
Dim cnt As Integer = 0
...

Notice you don't need to define an object's type if you instantiate it on the same line; the type is inferred from the assignment.
 
Thank you so much, I would like to access the most recent element pushed on the stack. Excuse me, I didn't refered it. Especially, I would like to access a class property of the most recent element pushed on the stack. As I said before, every element of stack is a class object created by me. It's very important for me to store all the stacks in a kind of structure.
I would like to hold stacks that some of them would modify some of it's elements (adding or deleting them) through runtime, or some of the stacks could be destructed and creating news through runtime. It's very important for me, to access the last item that pushed on each stack. I don't know if I should use another structure than collections.arraylist to hold all the stacks. I am complicated about. Any help will be much appreciated.
 

To access the last item pushed onto a stack you can use Pop() or Peek() depending on whether or not you'd like to remove the item from the stack. Pop() removes the item, Peek() allows you to inspect the item without removing.


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Thank you MarkSweetLand!!!
That helps me a lot, because I will store it in an instance object, so that I will be able to use it's properties!!!!
I would like to ask something else too, if it's possible, Is it possible to compare 2 or more stacks if they're equal as whole objects, or do I have to compare them element by element somehow????? Thank you very much in advanced too!!!!!!!!!!!!
 

Depending on what "equal" means. If you're comparing the stacks by the count of objects each holds, then yes. But if you need to determine if each stack holds the exact same object, then I would think you'd need to examine one by one since there is probably no guarantee that the objects are located in the same ordinal position in the stack.




Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Thank you once more Mark!!!!!

I meant to determine if each stack holds the exact same object with another. Then, would it be a good idea to declare 2 arraylists that would hold each stack so that to compare them, if they include the same objects even their ordinal position? What do you think???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top