• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays and stack

 
jai ahuja
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tell me the situation where stacks are better than arrays?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34850
369
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are different data structures. A stack is good for when you want to retrieve the last element added. An array is when you want to access any element at any time.
 
Amit Mmf Shrivastava
Greenhorn
Posts: 4
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jai,

I understand your question, that you want to know where Stacks is better than Arrays, as well as I read the comment of Jeanne as:

Jeanne Boyarsky wrote:They are different data structures. A stack is good for when you want to retrieve the last element added. An array is when you want to access any element at any time.


He is right, also I want to add some thing in his statement that size of stack is not limited while in array it is limited. And implementing a stack as an array is a bit more straightforward since we only need to keep track of the last element. We don't need to consider shunting items towards the head anymore, but we must still accommodate for new items which means we still waste memory allocating unused elements.

Regards
Amit
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Mmf Shrivastava wrote:He is right...

Jeanne is a "she".
 
Amit Mmf Shrivastava
Greenhorn
Posts: 4
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne is a "she"


I am so Sorry for this...
Regards
Amit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic