Madusudanan
Madusudanan

Reputation: 1017

Array based stack implementation?

I am new to the concept of data structures and I have created a Stack for my own usage.

public class ArrayBasedStackImpl<T> {

@SuppressWarnings("unchecked")
private T[] DataStack= (T[]) new Object[10];
private int stack_pointer=-1;

public void push(T t) throws Exception
{
if(stack_pointer>9)
{
throw new Exception("Stack is full");
}

DataStack[++stack_pointer]=t;
}

public T pop() throws Exception
{
if(stack_pointer==-1)
{
throw new Exception("Stack Empty");
}
else
{
return DataStack[stack_pointer--];
}
}

public T peek()
{
return DataStack[stack_pointer];    
}}

I have used an Array as a backend storage to start with.Before actually implementing it in full scale,there are a few questions that I have below

  1. Is it a good idea to use throw new Exception, or should I be writing my own exception class for this,considering the case that the stack is memory limited.

  2. Is Array actually a good idea for a Stack based implementation,my usage scenario is where I push 10 objects of considerable size into it,so I want to be able to free memory once I pop an item of the stack.How can I delete an item from an array,I googled much but couldnt find anything good.Will a linked list be better in this place when I want to clear it from memory.

  3. I am using generics here,is it a bad practice to use Array with Generics?

Upvotes: 1

Views: 923

Answers (2)

Sanjaya Liyanage
Sanjaya Liyanage

Reputation: 4746

It is always good to throw light exceptions than the Exception itself.And regarding using Array is good because you can have the exact number of elements you want and it will also an advantage for low memory usage as well.Sorry I don't have idea about the genic arrays. This might help

Upvotes: 0

Kyle
Kyle

Reputation: 4298

1) You shouldn't be throwing any Exception at all for "memory limitations". Simply build a bigger array. I recommend using an ArrayList or something that does this for you instead. The JVM will handle cases where you run out of memory, don't try to handle it yourself. If you are imposing a hard limit on memory usage (e.g. "the stack can only be as big as the initial capacity") then you can create your own Exception if you want to, such as StackFullException. My advice though, would be that the Stack should not have a size limit.

2) An array (or ArrayList) is fine. A Stack is a last in, first out data structure, so an ArrayList or array will work perfectly - simply pull the last item off of the array. Java does not have the same concept of "free"ing memory like C does, the garbage collector handles freeing memory. Once you are finished with an Object you simply get rid of references to it - for example, if you are using an array, you would set array[lastIndex] = null; once you did a pop() operation, and the garbage collector would handle the rest.

3) It is good practice, you are being specific about type information.

Upvotes: 1

Related Questions