Solved

For objects: Followup to  IntQueue Class implementation

Posted on 2003-11-26
21
254 Views
Last Modified: 2012-05-04
public int getFront()
{
Integer result = (Integer) data.get(front);
front++;
 return result.intValue();
}

I keep getting arrayoutofbound exception, how do I fix this ?
0
Comment
Question by:azcalv408
  • 10
  • 5
  • 4
  • +1
21 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9825707
As objects said;

"Though you probably should do a test to ensure you haven't reached end of queue"

so you need:

public int getFront()
{
    if( front < 0 || front > data.size() - 1 )
    {
        System.err.println( "Fell off the end of the data...  Wrapping round to the beginning" ) ;
        front = 0 ;
    }
    Integer result = (Integer) data.get(front);
    front++;
    return result.intValue();
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825784
>>
Integer result = (Integer) data.get(front);
front++;
return result.intValue();
>>

if you're intending there to alter result, you're not doing, btw
0
 
LVL 92

Expert Comment

by:objects
ID: 9827125
public int getFront()
{
   if (front==-1)
   {
      throw new RuntimeException("Queue is empty");
   }
   Integer result = (Integer) data.get(front);
   front++;
   if (front>=data.size())
   {
      front = -1;
   }
   return result.intValue();
}
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 92

Expert Comment

by:objects
ID: 9827141
Actually not sure I like that
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9827151
This should work better:

public int getFront()
{
   if (front>=data.size())
   {
      throw new RuntimeException("Queue is empty");
   }
   Integer result = (Integer) data.get(front);
   front++;
   return result.intValue();
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9827286
You seem to be confusing the items that are being queued and the question of indexes/where the front is. From the spec in your previous question, there seems to be no assumptions made about what type of item is being queued, so the following

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

doesn't seem to make much sense
0
 
LVL 92

Expert Comment

by:objects
ID: 9827422
Not sure I follow, and whats that got to do with dealing with an empty queue?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9827554
Working from memory (i no longer have the previous code open) the only methods that can affect where front is are the constructor and insert (and the latter sets the position of front). Therefore, the following should be quite adequate

public int getFront()
{
   if (data.size() == 0)
   {
      throw new RuntimeException("Queue is empty");
   }
   return front;
}

0
 

Author Comment

by:azcalv408
ID: 9827847
>>if (data.size() == 0)
   {
      throw new RuntimeException("Queue is empty");
   }
 >>  return front;

I think getFront() should return an element of the index front, not the index itself.
0
 
LVL 92

Expert Comment

by:objects
ID: 9827864
> I think getFront() should return an element of the index front, not the index itself.

That is correct, and should also increment the index.
The code I posted above should do the trick, let me know if you have any problems with it.
And if so, also post the code you are testing it with.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9827985
Well, i wasn't sure whether you wanted the index or the object. In that case the function should return an Object, not an int (or an Integer).
0
 
LVL 92

Expert Comment

by:objects
ID: 9828153
> should return an Object

That'd be a bit pointless for an IntQueue.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9828210
Well i don't remember seeing what you're meant to be queueing. If it's just numbers and you're not actually removing anything, merely resetting a pointer to the front of the queue, then you may as well just use an int[]. It's a bit pointless using a collection class
0
 

Author Comment

by:azcalv408
ID: 9828217
>>public int getFront()
{
   if (front>=data.size())
   {
      throw new RuntimeException("Queue is empty");
   }
   Integer result = (Integer) data.get(front);
   front++;
   return result.intValue();
>>}

objects, your code doesn't seem to work, here's the main test program:
public static void main(String[] args)
{
IntQueue queue1 = new IntQueue();
for(int i = 0; i < 20; i++)
queue1.insert(i);
System.out.println("\n now queue1 = " +queue1);
for(int j = 0; j < 15; j++)
queue1.getFront();
System.out.println("\n now queue1 = " +queue1);
}

the second part of queue1, after the second for loop, queue1 should be [15, 16, 17, 18, 19], but the program gives [0, 1, 2, 3, 4, 5, 6, 7..., 19], same as the first output. Can you see why ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9828236
Think the problems in your toString() method. It should only print elements from front onwards.
0
 

Author Comment

by:azcalv408
ID: 9828255
oh this is all I have for toString
    public String toString()
    {
      String answer = (data).toString();
          return answer;
    }

how do i print from front to end ?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 9828263
should be:

    public String toString()
    {
         String answer = data.subList(front, data.size()).toString();
         return answer;
    }
0
 

Author Comment

by:azcalv408
ID: 9828332
objects, sorry to bug you again but I wrote an equals method and it doesnt seem to work:
 public boolean equals(Object obj)
 {
      if(obj instanceof IntQueue)
      {
                 IntQueue queue = (IntQueue) obj;
                 return queue.data.subList(front,data.size()).equals
                (this.data.subList(front,data.size()));
      }
      else
      return false;
 }
can you see what's wrong? thanks a lot
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 9828364
your comparing the same front and data in some cases. Try

public boolean equals(Object obj)
 {
     if(obj instanceof IntQueue)
     {
                IntQueue queue = (IntQueue) obj;
                return queue.data.subList(queue.front,queue.data.size()).equals
               (this.data.subList(front,data.size()));
     }
     else
     return false;
 }
0
 
LVL 92

Expert Comment

by:objects
ID: 9828440
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9830253
:-(
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 37
oracle 11g 23 84
iterator/ListIterator approach 17 27
sql import cannot be resolved jsp 3 23
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…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 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