aspose file tools
The moose likes Beginning Java and the fly likes add and remove methods defined in arrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "add and remove methods defined in arrayList" Watch "add and remove methods defined in arrayList" New topic

add and remove methods defined in arrayList

Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Dear All,

I am trying to use the standard add and remove methods within the arrayList class but I think there is something I do not understand

/** Method to add a roadVehicle to the arrayList*/
public void add(int index, RoadVehicle roadVehicle)
throws QueueFullException{
if (isFull())
{throw new QueueFullException(queue.size());}
count ++;

/**Method to remove a roadVehicle from the arrayList*/
public RoadVehicle remove (int index)
throws RuntimeException {
if (isEmpty())
throw new RuntimeException ("RuntimeExceptio: attempting to remove from empty queue");
count = count-1;
return remove(0);

The add method seems to work when I use the following

queue1.add(0, car1);
queue1.add(1, car2);
queue1.add(2, bus1);
queue1.add(3, bus2);

So adding, at a specific element, different types of roadVehicle to the Trafficqueue instance queue.

The remove() method which I have defined as removing from the front of the queue, and believe the method will then shuffle all of the roadVehicles up the queue.

But when I try to remove an element from the queue using

I get the following error
---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" remove(int) in TrafficQueue cannot be applied to ()
1 error
> Terminated with exit code 1.

I think this may refer to the fact that I am trying to remove a roadVehicle from the queue using an int, but I thought that is what this method was supposed to do ie remove what ever is in the element defined by the method.

Second point
I have seen other code which references elements in an arrayList using a different syntax queue[0]as define for arrays. Can anyone tell me if this is correct and when you can use this as opposed to queue()?

Best wishes
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
ArrayList has two methods named remove:

public Object remove(int index)
public boolean remove(Object o)

There is no remove that takes no arguments. If you want to remove the first elements, write queue.remove(0). Also, when adding elements, if you are adding to the end of the list, it simpler to write queue.add(car) and not worry about tracking the count. And speaking of the count, ArrayList has a size() method, so you may be duplicationg effort by mantaining a count field. Finally, given the name of your field -- queue -- and the fact that you seem to want a FIFO queue -- add to the back and remove from the front, it may make more sense to use java.util.LinkedList: it has method removeFirst (as well as addLast). And if you are using the current version of Java, LinkedList implements a specific Queue interface, which you may be interested in.

There is no emoticon for what I am feeling!
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
The remove() method that you wrote above has more problems than what Jeff mentioned. Notice the line near the end of the remove method that says:

Since you didn't provide an object ref for the method "this" is assumed. So this.remove(0) will be called. Since this statement is found in the definition of the remove method it becomes a recursive call.

Depending on how isEmpty() is implemented (overridden or default implementation) your remove method will either run forever or count down the counter to 0 and then throw the RuntimeException. The easy fix for this is to change the statement in question to:

As Jeff mentioned, that might not be the desired behavior and you should consider a different underlying queue data structure.


Mike Noel
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Thanks to you both for your help.

I now have a "queue" that removes from the front and adds to the back. I think I did get a "recursive call" in one of the versions of remove() as I needed to stop this by control etc.

Unfortunately I am not allowed to change this "queue" to a linked list as I am supposed to be understanding the implementation of an array and how to alter the code to an arrayList.

Best wishes
I agree. Here's the link:
subject: add and remove methods defined in arrayList