Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

array out of bound problem

Posted on 2003-12-04
9
Medium Priority
?
259 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 750 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

722 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