So having one class for each of these concepts fits better to this than mixing up two of those concepts in one class, even it would technically be possibleĪny additional feature in a class requires additional effort to specify it, implement it, test it, review it, document it, release it and maintain it. Historically, arrays, lists and stacks are usually taught as different concepts in most CS courses, and the terminology which includes Push, Peek, and Pop is exclusively used the context of stacks. Mixing up two concepts in one class does not make the class more readable and understandable. But I can imagine the follwing potential reasons: Net framework designers not provide such a method directly as part of the List class? For getting a binding answer, you need to ask those library designers. I understand your question in this sense: why did the. (Similarly, you could implement something like a PopAt operation, which means a RemoveAt with returning the removed value.) Of course, the equivalent of a Pop() operation on a List requires a little bit more code, but if that really bothers you, because in your specific context you need that operation frequently, you can write an extension method for it, like this: static class ListExtensions Since a List allows one effectively to execute all stack-like operations, if one really needs "both concepts in one", using a List allows this, so this is IMHO not a "weaker" concept. Many thanks to the community, especially to user of having 2 weaker concepts and having to convert between them Update : Wow !! Thanks to this thread Microsoft had decided to add Pop to C# list !!. Why do we need a C# Stack when List could easily do all Stack does, and more?Įasy way to pop 'd make me always use List, hence saving the need of conversions/casting between List and Stack.Īm i the only C# dude, that wants a (slightly) more powerful List ? ( instead of having 2 weaker concepts and having to convert between them ). As List is implemented using array and supports Add in O(1) ( Add is just a different name for Push), why wasn't Pop added as well? List! It's convenient to use just one language construct, and avoid conversions, etc. Looking at Python, Perl, Ruby and 'e', one does wonder how come a C# List doesn't have Pop(). Why doesn't RemoveAt() return a value? What damage would happen if it returned a value (as actually done in Java ArrayList)? Starting with Stack, maybe later as problem changes, I'll need to access an, alternatively w/Lists, maybe later need Pop or List1.Add (List2.Pop()) often. (Implementing Pop for List, would make it 2-in-1! ).Ĭoming from more flexible languages, I don't want to commit to a limiting-collection-type. Why should developers choose, List or Stack, while in fact they can have "2 in 1". Following are the methods that are at our disposal when we use the stack class in Java.List is implemented in C# exactly as Stack, see: In Java, there are mainly 5 methods of Stack Class. Moving ahead, let’s see the different methods of Java Stack Class. Whenever you create a Stack, initially it does not contain any item, i.e, the Stack is empty. A stack in Java extends the vector class which further implements List interface. In the above hierarchy, the blue box refers to the different interfaces and the yellow box defines the class. Now, let’s have a clear view of how stack class in Java is arranged in the Java collections framework hierarchy. We know that Java collection framework includes interfaces and classes. Java Stack Class falls under the basic Collection Hierarchy Framework in which you can perform the basic operations such as push, pop, etc. What is a Stack Class in Java?Ī stack is a data structure which follows LIFO (Last In First Out). Following pointers will be explored in this article: In this article we explore this concept in detail. Stack class in Java is a part of Collection framework that simplifies various operations like push, pop, etc. Data Structures have been a boon to the programming world as they simplify programming to a great extent.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |