Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In This Project, Especially To Test The Int NextIndex() And Int PreviousIntex() Methods, My Client Class Is Like This Below In this project, especially to

In This Project, Especially To Test The Int NextIndex() And Int PreviousIntex() Methods, My Client Class Is Like This BelowIn this project, especially to test the int nextIndex() and int previousIntex() methods,

my client class is like this below:

The output with testing int nextIndex(and int previousIntex() methods is like this below

 

Can anybody help my code count the index value in the backward or forward direction??

Here is my entire code except client class (I already posted the client class above)

Those are the ArrayList and ArrayListIterator class.

ArrayList.java

==========================

package arraylist;

import java.util.Arrays;
import java.util.Iterator;

public class ArrayList {

private E[] elementData;
// current number of elements in the list
private int size;
public static final int DEFAULT_CAPACITY = 100;

public ArrayList() {
this(DEFAULT_CAPACITY);
}

@SuppressWarnings("unchecked")
public ArrayList(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity: " + capacity);
}
elementData = (E[]) new Object[capacity];
size = 0;
}

public int size() {
return size;
}

public E get(int index) {
checkIndex(index);
return elementData[index];
}

public String toString() {
if(size ==0) {
return "[]";
}
else {
String result = "[" + elementData[0];
for(int i =1;iresult += ", " +elementData[i];
}
result +="]";
return result;
}
}

public int indexOf(E value) {
for(int i=0;iif(elementData[i].equals(value)) {
return i;
}
}
return -1;
}

public boolean isEmpty() {
return size ==0;
}

public boolean contains(E value) {
return indexOf(value) >=0;
}

public boolean containsAll(ArrayList list) {
for(int i=0;iif(!contains(list.elementData[i])) {
return false;
}
}
return true;
}

public void add(E value) {
ensureCapacity(size + 1);
elementData[size] = value;
size++;
}

public void add(int index, E value) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index: " + index);
}
ensureCapacity(size + 1); //this public method throws the exception
for(int i = size; i>=index+1; i--) {
elementData[i] = elementData[i-1];
}
elementData[index] = value;
size++;
}

public void addAll(int index, ArrayList list) {
ensureCapacity(list.size+1);
for(int i=0; iadd(index,list.elementData[i]);
index++;
}
}

public void remove(int index) {
checkIndex(index);
for(int i = index; ielementData[i] = elementData[i+1];
}
elementData[size-1]=null;
size--;
}

public void removeAll(ArrayList list) {
for(int i=0; iint index = indexOf(list.elementData[i]);
if(index>0)
remove(index);
}
}

public void set(int index, E value) {
checkIndex(index);
elementData[index] = value;
}

public void clear() {
for(int i=0; ielementData[i] = null;
}
size = 0;
}

public void addAll(ArrayList other) {
ensureCapacity(size + other.size);
for(int i=0; iadd(other.elementData[i]);
}
}

public Iterator iterator(){
return new ArrayListIterator2(this);
}

public void ensureCapacity(int capacity) {
if (capacity > elementData.length) {
int newCapacity = elementData.length * 2 + 1;
if (capacity > newCapacity) {
newCapacity = capacity; }
elementData = Arrays.copyOf(elementData, newCapacity);
}
}

public void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("index: " + index);
}
}

}

=================================

ArrayListIterator2.java

=================================

package arraylist;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayListIterator2 implements Iterator {
private int position;
private boolean removeOK;
private ArrayList list; // create a instance of array list class

public ArrayListIterator2(ArrayList list) {
this.list = list;
position = 0;
}

// Returns true if this list iterator has more elements when traversing the list in the forward direction
public boolean hasNext() {
return position < list.size();
}

// Returns the next element in the list and advances the position
public E next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
E result = list.get(position);
position++;
removeOK = true;
return result;
}

// Returns true if this list iterator has more elements when traversing the list in the reverse direction
public boolean hasPrevious() {
return position > 0;
}

// Returns the previous element in the list and moves the position backwards
public E Previous() {
if(!hasPrevious()) {
throw new NoSuchElementException();
}
position--;
E result = list.get(position);
removeOK = true;
return result;
}

// Inserts the specified element into the list
public void add(E value) {
list.add(position, value);
position++;
removeOK = false;
}

// Returns the index of the element that would be returned by a subsequent call to next method
public int nextIndex() {
return position;
}


// Returns the index of the element that would be returned by a subsequent call to previous method
public int previousIndex() {
return position -1;
}


// Removes from the list the last element that was returned by next() or previous()
public void remove() {
if(!removeOK) {
throw new IllegalStateException();
}
position--;
list.remove(position);
removeOK = false;
}

// Replaces the last element returned by next() or previous() with the specified element
public void set(E value) {
if(!removeOK) {
throw new IllegalStateException();
}
list.set(position, value);
}
}


The java.util package has an interface called ListIterator that extends the Iterator interface and includes additional methods specific to iterating through the elements of lists forward or backward. Write a class called ArrayListIterator 2 that adds some or all of these methods. The methods to add are as follows (see the Java API Specification for descriptions of each method): public void add(E value) public boolean hasPrevious() public int nextIndex() public E previous () public int previousIndex() - public void set (E value)

Step by Step Solution

3.45 Rating (161 Votes )

There are 3 Steps involved in it

Step: 1

Here simply change your nextInd... 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

Recommended Textbook for

Seeing Through Statistics

Authors: Jessica M.Utts

4th Edition

1285050886, 978-1305176249, 1305176243, 978-1305322394, 978-1285050881

Students also viewed these Accounting questions