Solved

array out of bound problem

Posted on 2003-12-04
9
242 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
  • 5
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
> if(data.size() == 0)

that should be:

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

Expert Comment

by:jimmack
Comment Utility
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
Comment Utility
Sorry, I missed a bit on my last comment:

With this method, you don't need to store (or manage) the "front" attribute.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
> 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 15

Expert Comment

by:jimmack
Comment Utility
>> 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
Comment Utility
> If data.size() == 0

then the first test will be true.
0
 
LVL 15

Expert Comment

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

Expert Comment

by:objects
Comment Utility
The day is young here :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now