473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ArrayList removeFirst method

I am trying to write a removeFirst method for an ArrayList of the generic type. The removeFirst method is supposed to do a few things: 1) If the list is empty it throws an error. 2) It stores the data in a node pointed to by head. 3) Set the head to head's next. 4) decrement the size. 5) returns the data. I can do 1,4, and 5 but I am stuck on 2 and 3. Here is the code I have so far. Please take a look and give me a hint or suggestion:
================================================== =======
public class ArrayList<T> implements ListADT<T>
{
protected final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
protected int rear;
protected T[] list;


//-----------------------------------------------------------------
// Creates an empty list using the default capacity.
//-----------------------------------------------------------------
public ArrayList()
{
rear = 0;
list = (T[])(new Object[DEFAULT_CAPACITY]);
}

//-----------------------------------------------------------------
// Creates an empty list using the specified capacity.
//-----------------------------------------------------------------
public ArrayList (int initialCapacity)
{
rear = 0;
list = (T[])(new Object[initialCapacity]);
}

//-----------------------------------------------------------------
// Removes and returns the first element in the list.
//-----------------------------------------------------------------

public T removeFirst()throws EmptyCollectionException
{
if (rear == 0)
throw new EmptyCollectionException("Trying to remove first element");


return result;

}
//-----------------------------------------------------------------
// Removes and returns the specified element.
//-----------------------------------------------------------------
public T remove (T element)
{
T result;
int index = find (element);

if (index == NOT_FOUND)
throw new ElementNotFoundException ("list");

result = list[index];
rear--;
// shift the appropriate elements
for (int scan=index; scan < rear; scan++)
list[scan] = list[scan+1];


list[rear] = null;

return result;
}


//-----------------------------------------------------------------
// Returns a reference to the element at the front of the list.
// The element is not removed from the list. Throws an
// EmptyCollectionException if the list is empty.
//-----------------------------------------------------------------
public T first() throws EmptyCollectionException
{ return result;
}

//-----------------------------------------------------------------
// Returns a reference to the element at the rear of the list.
// The element is not removed from the list. Throws an
// EmptyCollectionException if the list is empty.
//-----------------------------------------------------------------
public T last() throws EmptyCollectionException
{ return result;
}

//-----------------------------------------------------------------
// Returns true if this list contains the specified element.
//-----------------------------------------------------------------
public boolean contains (T target)
{
return (find(target) != NOT_FOUND);
}

//-----------------------------------------------------------------
// Returns the array index of the specified element, or the
// constant NOT_FOUND if it is not found.
//-----------------------------------------------------------------
private int find (T target)
{
int scan = 0, result = NOT_FOUND;
boolean found = false;

if (! isEmpty())
while (! found && scan < rear)
if (target.equals(list[scan]))
found = true;
else
scan++;

if (found)
result = scan;

return result;
}

//-----------------------------------------------------------------
// Returns true if this list is empty and false otherwise.
//-----------------------------------------------------------------
public boolean isEmpty()
{
return (rear==0);
}

//-----------------------------------------------------------------
// Returns the number of elements currently in this list.
//-----------------------------------------------------------------
public int size()
{
return rear;
}

//-----------------------------------------------------------------
// Returns an iterator for the elements currently in this list.
//-----------------------------------------------------------------
public Iterator<T> iterator()
{
return new ArrayIterator<T> (list, rear);
}

//-----------------------------------------------------------------
// Returns a string representation of this list.
//-----------------------------------------------------------------
public String toString()
{
String result = " ";

for (int index = 0; index < rear; index++)
result = result + list[index].toString() + "\n";

return result;
}

//-----------------------------------------------------------------
// Creates a new array to store the contents of the list with
// twice the capacity of the old one.
//-----------------------------------------------------------------
protected void expandCapacity()
{
T[] larger = (T[])(new Object[list.length*2]);

for (int scan=0; scan < list.length; scan++)
larger[scan] = list[scan];

list = larger;
}
}
Feb 22 '07 #1
7 3794
DeMan
1,806 1GB
It appears you are implementing the whole type?

While implementing it with an array will work, can I suggest that you try to use an actual linked list. Java is reasonably simple for this, and you will find that insert and remove methods will be more efficient...
Unless you are required to use an array for this , I would try to avoid it....
Feb 22 '07 #2
The 2nd part of the project is to do the same methods with a LinkedList in mind so that we can see the time variance between the two with a sample driver class that the teacher has produced. My problem right now is that I am lost on writing the removeFirst method.
Feb 22 '07 #3
DeMan
1,806 1GB
Inefficient I know....

Since you are using arrays, you know that this element is in list[0], and we don't really even have to delete him, we can copy list[1] over him, list[2] over list[1] ETC...

Pseudocode (probably more javanese than anything)
Expand|Select|Wrap|Line Numbers
  1. for(i=0 to list.size-1) //or length 
  2. {
  3.   list[i]=list[i+1]
  4. }
  5. list[list.size] = NULL; //or zero or whatever you need to do to delete
  6.  
Feb 22 '07 #4
This is what I came up with on my own, tell me what you think.
================================================== ====

public T removeFirst()throws EmptyCollectionException
{
T result;
if (rear == 0)
throw new EmptyCollectionException("Trying to remove first element");
result = list[rear];
rear --;

return result;

}
Feb 22 '07 #5
DeMan
1,806 1GB
Depending on how you implement your list (like if you stored the first element at the bac) that would work. Can I just clarify that we ARE trying to create a list structure? Because I sus[ect you're trying to create a stack......

Also, since I'm being Pinickity (??) can I suggest that you enclose your code in code tags for easier reading (by using the # button above).
Feb 23 '07 #6
Sorry about the long code I displayed. Anyway, I figured it all out, but thanks for the help.
Feb 25 '07 #7
r035198x
13,262 8TB
Sorry about the long code I displayed. Anyway, I figured it all out, but thanks for the help.
Posting long code is not a problem as long as you enclose it in code tags.
Feb 26 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
4
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties...
1
by: Sylvain | last post by:
Hi, I'm encountering a very simple issue with ArrayList constructor and AddRange() method overriding. I'm defining a class that extends ArrayList and contains one overriden method:...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
5
by: blt51 | last post by:
I need to write a program that handles a bank account and does a number of transactions using an arraylist. However, I'm having trouble getting the arraylist to store all the transactions and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.