/**
* <p>Title:
</p>
*
<p>Description: </p>
* <p>Copyright:
Copyright (c) 2006</p>
* <p>Company:
</p>
* @author not
attributable
* @version 1.0
*/
public class AList implements ListInterface
{
private
Object[] entry; // array of list entries
private int
length; // current number of entries
in list
private static
final int MAX_SIZE = 50; // max length of list
public AList()
{
length
= 0;
entry
= new Object[MAX_SIZE];
} // end
default constructor
public
AList(int maxSize)
{
length
= 0;
entry
= new Object[maxSize];
} // end
constructor
public boolean
add(Object newEntry)
{ /* < Implementation deferred > */
boolean
isSuccessful = true;
if (!isFull())
{
//
position of new entry will be after last entry in list,
//
that is, at position length+1; corresponding array index is
// 1
less than position, so index is length
entry[length] = newEntry;
length++;
}
else
isSuccessful = false;
return
isSuccessful;
} // end add
public boolean
add(int newPosition, Object newEntry)
{
boolean
isSuccessful = true;
if (!isFull()
&& (newPosition >= 1)
&& (newPosition <= length+1))
{
makeRoom(newPosition);
entry[newPosition-1] = newEntry;
length++;
}
else
isSuccessful = false;
return
isSuccessful;
} // end add
public Object
remove(int givenPosition)
{
Object result
= null; // return value
if
((givenPosition >= 1) && (givenPosition <= length))
{
result
= entry[givenPosition-1]; // get entry to be removed
//
move subsequent entries toward entry to be removed,
//
unless it is last in list
if
(givenPosition < length)
removeGap(givenPosition);
length--;
} // end if
// else
// throw new IllegalStateException("Given
position in the list is illegal.");
return
result; // return reference to removed
entry,
// or null if givenPosition is invalid
} // end remove
public void
clear()
{
length
= 0;
//< But see Question 8. >
} // end clear
public boolean
replace(int givenPosition, Object newEntry)
{
boolean
isSuccessful = true;
if
((givenPosition >= 1) && (givenPosition <= length))
entry[givenPosition-1] = newEntry;
else
isSuccessful = false;
return
isSuccessful;
} // end replace
public Object
getEntry(int givenPosition)
{
Object result
= null; // result to return
if
((givenPosition >= 1) && (givenPosition <= length))
result
= entry[givenPosition-1];
return result;
} // end getEntry
public boolean
contains(Object anEntry)
{
boolean found
= false;
for (int index
= 0; !found && (index < length); index++)
{
if
(anEntry.equals(entry[index]))
found = true;
} // end for
return found;
} // end contains
public int
getLength()
{
return
length;
} // end
getLength
public boolean
isEmpty()
{
return
length == 0;
} // end
isEmpty
public boolean
isFull()
{
return
length == entry.length;
} // end
isFull
public void
display()
{
for
(int index = 0; index < length; index++)
System.out.println(entry[index]);
} // end
display
/* < This
class will define two private methods that will be discussed later. > */
/** Task:
Makes room for a new entry at newPosition.
* Precondition: 1 <= newPosition <=
length+1;
* length is listās length before
addition. */
private void makeRoom(int newPosition)
{
// move each
entry to next higher position, starting at end of
// list and
continuing until the entry at newPosition is moved
for (int index
= length; index >= newPosition; index--)
entry[index] = entry[index-1];
} // end makeRoom
/** Task:
Shifts entries that are beyond the entry to be removed
* to next lower position.
* Precondition: 1 <= givenPosition <=
length;
* length is listās length before
removal. */
private void removeGap(int givenPosition)
{
for (int index
= givenPosition; index < length; index++)
entry[index-1]
= entry[index];
} // end removeGap
} // end AList