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.
So you set a default capacity of 10? And where are you enlarging your array when you pass the 10th element?
Joined: Nov 28, 2012
I'm not, the class variables were declared before we started. We only implemented the methods. I had a feeling that was the issue but the test program was throwing up data larger than 10 so do you think there could be an error in the testing data sizes?
Joined: Nov 28, 2012
I've actually made a few changes to improve the code. I actually code the enqueue and dequeue the wrong way round. The rear was the last element in the queue therefore if would be the first to be removed. 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.. Anyone could help with that?
Arrays in Java are static in size: once you declare them to have a certain number of elements, that's it. They don't automatically grow. If you use the no-argument constructor, then your array will only have 10 elements. If you use the constructor that takes an initial capacity, the array will only have that many elements. As Campbell alluded to before, there is nothing in your code that increases the size of your array once it has been filled up. Incrementing the 'size' variable doesn't do it.
If you fill the array from the 0 index you don't need to use rear, front and size variables, you just need a single variable to denote the next available location in the stack. The variable starts at 0 and provides the index to insert the next enqueued element and is then incremented. It's value also gives you the current stack size. And to dequeue an element you decrement the variable and remove and return the element at that index. Obviously, you also need to do bounds checking before adding or removing elements.
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.
Joined: Oct 13, 2005
stefan balling wrote: . . . an ArrayQueue. It should act as a stack basically . . .
In which case, why are you calling it ArrayQueue? Stacks are Stacks and Queues are Queues and the two behave differently.
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.