1

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?

6
  • 2
    Why are you not using java.util.Stack? Commented Apr 30, 2013 at 6:18
  • It uses the vector class,I dont want to synchronize on each and every operation. Commented Apr 30, 2013 at 6:18
  • You do realise that the source is available, don't you? Commented Apr 30, 2013 at 6:20
  • Of course yes,but I dont want to get into a discussion on which collection is best suited for stack implementation,but I want to know what are all the problems with the above mentioned implementation. Commented Apr 30, 2013 at 6:22
  • It would seem easier to simply swap the Vector out for an unsynchronised List implementation, but, it's your time. Commented Apr 30, 2013 at 6:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

By the way, there is already a StackOverflowException I think. I said StackFullException just to demonstrate that yes, you can create custom Exception classes, and no, there is nothing wrong (in general) with doing so.
0

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

2 Comments

Hi sanjay,can you tell me more about light exceptions.
Hi Optimus, What I meant by light exception is user defined exceptions. It will be easy to handle them as they are on to the specific matter.sorry if the word light did hesitate you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.