GeeCON Prague 2014*
The moose likes C / C++ and the fly likes arrays and stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Languages » C / C++
Bookmark "arrays and stack" Watch "arrays and stack" New topic
Author

arrays and stack

jai ahuja
Greenhorn

Joined: May 17, 2012
Posts: 4
tell me the situation where stacks are better than arrays?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Amit Mmf Shrivastava
Greenhorn

Joined: Oct 30, 2012
Posts: 4

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


PHP Training Indore | Java Training Indore | .Net Training Indore
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11350
    
  16

Amit Mmf Shrivastava wrote:He is right...

Jeanne is a "she".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Amit Mmf Shrivastava
Greenhorn

Joined: Oct 30, 2012
Posts: 4

Jeanne is a "she"


I am so Sorry for this...
Regards
Amit
 
GeeCON Prague 2014
 
subject: arrays and stack