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
Solved

array out of bound problem

Posted on 2003-12-04
9
247 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …

856 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