In This Project, Especially To Test The Int NextIndex() And

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);
}
}

 

Stressed over that homework?

Essay deadline breathing down your neck?

Let’s cut to the chase: Why struggle when you can ace it with zero hassle?

Whether it’s essays, research papers, or assignments — we’ve got you covered.

✅ Expert writers
✅ 100% original work
✅ No AI tools, just real pros

Stressed about your essay or homework? Get a top-quality custom essay NOW!!! Stop worrying. Start succeeding.

GradeEssays.com
We are GradeEssays.com, the best college essay writing service. We offer educational and research assistance to assist our customers in managing their academic work. At GradeEssays.com, we promise quality and 100% original essays written from scratch.
Contact Us

Enjoy 24/7 customer support for any queries or concerns you have.

Phone: +1 213 3772458

Email: support@gradeessays.com

© 2024 - GradeEssays.com. All rights reserved.

WE HAVE A GIFT FOR YOU!

15% OFF 🎁

Get 15% OFF on your order with us

Scroll to Top