Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

**** IN JAVA *** I need the listIterator function implemented. AList.java // this is where the listIterator function needs to be implemented. import java.io.Serializable; import

**** IN JAVA ***

I need the listIterator function implemented.

AList.java // this is where the listIterator function needs to be implemented.

import java.io.Serializable; import java.util.Arrays; import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException; public class AList implements List, Serializable { private Object[] list; private int size; private int capacity; public static final int DEFAULT_CAPACITY = 25; public AList() { this(DEFAULT_CAPACITY); } public AList(int initialCapacity) { capacity = initialCapacity; list = new Object[capacity]; } @Override public void add(int index, T obj) { if (obj == null) throw new NullPointerException(); if (index < 0 || index > size) throw new IndexOutOfBoundsException(); if (size == capacity) { resize(capacity * 2); } //shift the elements down to make room for the new element for (int i = size; i > index; i--) { list[i] = list[i-1]; } list[index] = obj; size++; } @Override public boolean add(T obj) { add(size, obj); return true; } @Override public void clear() { list = new Object[capacity]; size = 0; } @Override public boolean contains(T obj) { return indexOf(obj) != -1; } @Override public int indexOf(T obj) { for (int i = 0; i < size; i++) { if (list[i].equals(obj)) { return i; } } return -1; } @Override public boolean isEmpty() { return size == 0; } @Override public int lastIndexOf(T obj) { for (int i = size-1; i >= 0; i--) { if (list[i].equals(obj)) { return i; } } return -1; } @Override public T get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); return (T)list[index]; } @Override public T remove(int index) { T old = get(index); for (int i = index; i < size-1; i++) { list[i] = list[i+1]; } list[--size] = null; return old; } @Override public boolean remove(T obj) { int index = indexOf(obj); if (index < 0) return false; remove(index); return true; } @Override public T set(int index, T obj) { if (obj == null) throw new NullPointerException(); T old = get(index); list[index] = obj; return old; } @Override public int size() { return size; } @Override public Object[] toArray() { Object[] rarr = Arrays.copyOf(list, size); return rarr; } @Override public Iterator iterator() { return new Iterator() { private int current = 0; private boolean nextCalled = false; @Override public boolean hasNext() { return current < size; } @Override public T next() { if (!hasNext()) throw new NoSuchElementException(); T obj = (T)list[current++]; nextCalled = true; return obj; } @Override public void remove() { if (!nextCalled) throw new IllegalStateException(); current--; for (int i = current; i < size-1; i++) { list[i] = list[i+1]; } list[--size] = null; nextCalled = false; } }; } @Override public ListIterator listIterator(int index) { return null; } private void resize(int newCapacity) { list = Arrays.copyOf(list, newCapacity); capacity = newCapacity; } }

List.java (needed)

import java.util.ListIterator; public interface List extends Iterable { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** * If obj is in the list, return the * index of the first occurrence. * Otherwise, return -1. * @param obj * @return */ public int indexOf(T obj); public boolean isEmpty(); public int lastIndexOf(T obj); /** * Get and return the value stored at the index. * * @param index * @return * @throws IndexOutOfBoundsException */ public T get(int index); public T remove(int index); public boolean remove(T obj); public ListIterator listIterator(int index); /** * Update the value in the list at the specified index. * Return the old value * @throws IndexOutOfBoundsException * @param index * @param obj * @return */ public T set(int index, T obj); public int size(); public Object[] toArray(); }

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_2

Step: 3

blur-text-image_step3

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

More Books

Students explore these related Databases questions

Question

d. What language(s) did they speak?

Answered: 3 weeks ago

Question

e. What difficulties did they encounter?

Answered: 3 weeks ago