Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the SimpleArrayList class to provide the implementation of the missing methods in SimpleArrayListIterator and test the SimpleArrayList with DemoSimpleArrayListIter : SimpleArrayListIterator ( int index

Modify the SimpleArrayList class to provide the implementation of the missing methods in SimpleArrayListIterator and test the SimpleArrayList with DemoSimpleArrayListIter

:

SimpleArrayListIterator

(

int index

)

(the constructor)

boolean hasNext()

int nextIndex()

boolean hasPrevious()

E previous()

int previousIndex()

The meaning of the methods should be the same as of the corresponding ones provided by the interface java.util.List

Iterator

.

You must check for the concurrent modification error. Please, consult the Java SE API documentation ,as needed

import java.lang.UnsupportedOperationException;

import java.util.NoSuchElementException;

import java.util.ConcurrentModificationException;

import java.util.Arrays;

import java.util.ListIterator;

/**

* This class provides a simple generic list implmented as

* an array. It is similar to the ArrayList class included

* in the java.util package.

*

* The first element of the list is at position 0 and the

* last element is at position list.size() - 1.

*

* Except for the method next(), implementation of the

* SimpleArrayListIterator methods is left as part of your

* project.

*/

public class SimpleArrayList {

public static final int DEFAULTSZ = 20; // default array size

private Object[] list; // contents of the list

private int count; // number of the list elements

private int modCount = 0; // the total number of modifications

// (add and remove calls)

/**

* Creates an ampty SimpleArrayList with the default capacity

*/

public SimpleArrayList()

{

list = new Object[ DEFAULTSZ ];

count = 0;

}

public boolean isEmpty()

{

return count == 0;

}

public int size()

{

return count;

}

/**

* Adds an element at the end of the list.

* @param e the element to be added to the list.

* @return true always

*/

public boolean add( E e )

{

// make sure that the list has sufficient space

ensureCapacity( count + 1 );

list[ count ] = e;

count++;

modCount++;

return true;

}

/**

* Returns the element of the list at the indicated position.

* @param index the position of the list element to return.

* @return the element at position index.

* @throws IndexOutOfBoundsException if the index is < 0 or >= the size of the list.

*/

public E get( int index )

{

// check if the given index is valid

if( index < 0 || index >= count )

throw new IndexOutOfBoundsException( "Index value: " + index );

@SuppressWarnings("unchecked")

E e = (E) list[ index ];

return e;

}

/**

* Adds an element to the list at the specified position. It is possible to add

* a new element at the end of the list when index is equal to the size of the list.

* @param index the position where the element should be added.

* @param e the element to be added to the list.

* @return true

* @throws IndexOutOfBoundsException if the index out of range, i.e. index < 0 or index > the size().

*/

public boolean add( int index, E e )

{

// check if the given index is valid

if( index < 0 || index > count )

throw new IndexOutOfBoundsException( "Index value: " + index );

// make sure that the list has sufficient space

ensureCapacity( count + 1 );

// shift the contents of the array one spot to the right from index

System.arraycopy( list, index, list, index + 1, count - index);

list[ index ] = e;

modCount++;

count++;

return true;

}

public E remove( int index )

{

// check if the given index is valid

if( index < 0 || index >= count )

throw new IndexOutOfBoundsException( "Index value: " + index );

// get the element removed

@SuppressWarnings("unchecked")

E removed = (E) list[ index ];

// shift the contents of the array one cell to the left from index, but only if

// it is not the last cell

if( index < count - 1 )

System.arraycopy( list, index+1, list, index, count - index - 1 );

list[ --count ] = null;

modCount++;

return removed;

}

/**

* Returns the index of the first occurrence of a given element on the list equal

* or -1 if the given element is not on the list. If the argument element is null,

* the method returns the index of the first null elment on the list, or -1 if the list

* has no null elements.

* @param e the element to be located on the list.

* @return the index of the first occurrence of a given element on the list equal

* or -1 if the given element is not on the list.

*/

public int indexOf( E e )

{

if( e != null ) {

// look for the first non-null element equal to the argument e

for( int i = 0; i < count; i++ )

if( list[ i ] != null && list[ i ].equals( e ) )

return i;

}

else {

// look for the first null element, since the argument is null

for( int i = 0; i < count; i++ )

if( list[ i ] == null )

return i;

}

return -1;

}

/**

* Returns a ListIterator of the list elements, starting at the given position in this list.

* @param index the position of the first element on the list to be returned from the iterator.

* If the index is equal to the size of the list, the iterator starts at the end of the list.

* @return the created ListIterator

* @throws IndexOutOfBoundsException if the index is < 0 or > the size of the list.

*/

public ListIterator listIterator( int index )

{

if( index < 0 || index > count )

throw new IndexOutOfBoundsException( "Index value: " + index );

return new SimpleArrayListIterator( index );

}

// Make sure that the capacity of the list is large enough to accomodate

// sz number of elements. If not, enlarge the list array by allocating

// a larger array and copying the old content.

private void ensureCapacity( int sz )

{

Object[] newList = null;

// if the list is large enough, return immediately

if( list.length >= sz )

return;

// compute the next larger size, as a multiple of DEFAULTSZ

int newSize = (sz / DEFAULTSZ + 1) * DEFAULTSZ;

// create a new array and copy the old contents into it

list = Arrays.copyOf( list, newSize );

}

/**

* This class provides an iterator for the SimpleArrayList.

* It partially implements the java.util.ListIterator interface.

*

* Except for the method next(), implementation of the

* SimpleArrayListIterator methods is left as part of your

* project.

*/

private class SimpleArrayListIterator

implements ListIterator

{

private int currPos = 0; // index of the element to be returned next

private int expectedModCount = modCount;

// Creates a new iterator starting at position index.

// javadoc comment needed

public SimpleArrayListIterator( int index )

{

// replace with your code

}

// Returns true if this list iterator has more elements when traversing the list forward.

// javadoc comment needed

public boolean hasNext()

{

// replace with your code

return true;

}

// Returns true if this list iterator has more elements when traversing the list in the reverse direction.

// javadoc comment needed

public boolean hasPrevious()

{

// replace with your code

return true;

}

/**

* Returns the next element on the list.

* @return the next element on the list.

* @throw NoSuchElementException if the next element does not exist.

*/

@SuppressWarnings("unchecked")

public E next()

{

checkForComodification();

if( currPos >= count )

throw new NoSuchElementException();

// note the use of the "outer" class reference in SimpleArrayList.this

// to access the outer class

return (E) SimpleArrayList.this.list[ currPos++ ];

}

// Returns the index of the element that would be returned by a call to next.

// javadoc comment needed

public int nextIndex()

{

// replace with your code

return 0;

}

// Returns the previous element in the list.

// javadoc comment needed

@SuppressWarnings("unchecked")

public E previous()

{

// replace with your code

return null;

}

// Returns the index of the element that would be returned by a call to previous.

// javadoc comment needed

public int previousIndex()

{

// replace with your code

return 0;

}

// the following are optional operations which are not supported in the

// SimpleArrayList implementation; no modifications/additions are needed here

// Adds a new element

// not implemented here

public void add(Object e)

{

throw new UnsupportedOperationException( "add is not available" );

}

// Removes from the list the last element that was returned by next or previous (optional operation).

// not implemented here

public void remove()

{

throw new UnsupportedOperationException( "remove is not available" );

}

// Replaces the last element returned by next or previous with the specified element (optional operation).

// not implemented here

public void set(Object e)

{

throw new UnsupportedOperationException( "set is not available" );

}

// check if there was a concurrent modification of the list contents.

// if yes, throw a ConcurrentModificationException exception

private final void checkForComodification() {

if (expectedModCount != SimpleArrayList.this.modCount)

throw new ConcurrentModificationException();

}

}

}

import java.util.ListIterator;

public class DemoSimpleArrayListIter

{

public static void main(String[] args)

{

SimpleArrayList sList = new SimpleArrayList();

SimpleArrayList iList = new SimpleArrayList();

if( sList.isEmpty() )

System.out.println( "sList is empty: no elements: " + sList.size() );

else

System.out.println( "sList is not empty: no elements: " + sList.size() );

System.out.println( "Adding at end: A B C D E F" );

sList.add( "A" );

sList.add( "B" );

sList.add( "C" );

sList.add( "D" );

sList.add( "E" );

sList.add( "F" );

if( sList.isEmpty() )

System.out.println( "sList is empty: no elements: " + sList.size() );

else

System.out.println( "sList is not empty: no elements: " + sList.size() );

ListIterator iter = sList.listIterator( 0 );

System.out.print( "List: {" );

while( iter.hasNext() )

System.out.print( " " + iter.next() );

System.out.println( " }" );

System.out.print( "List in reverse: {" );

while( iter.hasPrevious() )

System.out.print( " " + iter.previous() );

System.out.println( " }" );

for( int i = 0; i < 110; i++ )

iList.add( i );

ListIterator iter2 = iList.listIterator( 0 );

System.out.print( "List: {" );

while( iter2.hasNext() )

System.out.print( " " + iter2.next() );

System.out.println( " }" );

if( iter.hasNext() )

System.out.println( "Step to the next: " + iter.next() );

if( iter.hasNext() )

System.out.println( "Step to the next: " + iter.next() );

// add an element

sList.remove( 3 );

if( sList.isEmpty() )

System.out.println( "sList is empty: no elements: " + sList.size() );

else

System.out.println( "sList is not empty: no elements: " + sList.size() );

if( iter.hasNext() )

System.out.println( "Step to the next: " + iter.next() );

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How many grams are in 0.65 mole of allyl sulfide?

Answered: 1 week ago