Solved

array out of bound problem

Posted on 2003-12-04
9
255 Views
Last Modified: 2010-03-31
I'm working on an IntQueue class and implemented a getFront() method to get the 1st element and remove it by incrementing the index. I kept getting an array out of bound error, can someone try to debug this ? thanks  (object- if you're reading this, i'm trying a new way of implementing this method)

// data = a vector
// front = the index
public int getFront()
{
       if(data.size() == 0)
       {
              throw new NoSuchElementException("Queue underflow");
       }

       Integer result = (Integer) data.get(front);
       front++;

       if(front > 10 && front > data.size()/2)
       {
                data = new Vector(data.subList(front, data.size()));
      front = 0;
       }

       return result.intValue();
}
0
Comment
Question by:azcalv408
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9878294
> if(data.size() == 0)

that should be:

if(front>=data.size())
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9878440
I'd suggest that the two tests need combining:

if ((front >= data.size()) || (data.size() == 0))

Is there any reason why you're not simply returning the first element in the Vector, then deleting it?  eg.

public int getFront()
{
    int result = -1;

    if (data.size() > 0)
    {
        result = ((Integer)data.get(0)).intValue();
        data.remove(0);
    }
    else
    {
        throw new NoSuchElementException("Queue underflow");
    }

    return (result);
}
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9878447
Sorry, I missed a bit on my last comment:

With this method, you don't need to store (or manage) the "front" attribute.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9878471
> if ((front >= data.size()) || (data.size() == 0))

Second test is redundant.

> Is there any reason why you're not simply returning the first element in the Vector,
> then deleting it?

this was discussed in a previous q.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9878530
>> this was discussed in a previous q.

Ah.

>> Second test is redundant.

If data.size() == 0, then the data.get(front) will throw an exception.  Specifically, the ArrayIndexOutOfBounds exception!

(Or was this discussed in the unreferenced question too ;-) ?)
0
 
LVL 92

Expert Comment

by:objects
ID: 9878558
> If data.size() == 0

then the first test will be true.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9878578
It's late.  Can you tell ;-) ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9878632
The day is young here :)
0
 
LVL 92

Expert Comment

by:objects
ID: 9879125
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question