I'm currently working on a uni exercise which I have finished but I keep getting an error when I compile an array greater than 10. I am creating an ArrayQueue. It should act as a stack basically and I was asked to implement these methods and test on data samples he gave me. Apologizes if I upload this wrong. My code is:
My error when I run an array of size 10 or above is an ArrayOutOfBounds exception. I know why that exception is cropping up in theory but I don't see where my code is causing this.
Thanks for any help.
BTW in future when posting, if you are getting an exception please include the full exception message and stack trace.
stefan balling wrote:The problem now is every time I add a new element in, I get the element plus the next element added as null. I.e I add cat and I get (cat, null) displayed..
The null isn't being added - the array elements will be null by default. The actual problem is your toString method that is printing out size + 1 elements.
In which case, why are you calling it ArrayQueue? Stacks are Stacks and Queues are Queues and the two behave differently.
stefan balling wrote: . . . an ArrayQueue. It should act as a stack basically . . .
There are all sorts of other style problems, for example using an if-else to return a boolean. Look here for what you ought to write.