Solved

array out of bound problem

Posted on 2003-12-04
9
243 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 53
Java SE 8u111  Lot of stuff broke 11 54
MySqlDump not dumping triggers 1 19
hibernate insert example 13 15
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …

863 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

20 Experts available now in Live!

Get 1:1 Help Now