Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***** IN JAVA ******* I need the rest of the methods within ListIterator in AList.java to be completed. I have done a few of them

***** IN JAVA *******

I need the rest of the methods within ListIterator in AList.java to be completed. I have done a few of them already to get the ball rolling, I just cannot figure out the rest of them really. Thank you in advance :)

Classes you WILL need:

AList.java

import java.io.Serializable; import java.util.Arrays; import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException;

/** * Implementation of an array list. In this implementation: * - The list can be resized. * - No null values allowed. */ 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; } }; } // here is where i need the methods to be completed. @Override public ListIterator listIterator(int index) { return new ListIterator(){ private int nextIndex = index; private boolean nextCalled = false, previousCalled = false;

@Override public boolean hasNext(){ return nextIndex < size; } @Override public T next() { if (hasNext()){ nextCalled = true; previousCalled = false; T obj = (T)list[nextIndex++]; return obj; } else { throw new NoSuchElementException(); } }

@Override public boolean hasPrevious() { return nextIndex > 0; }

@Override public T previous() { if (hasPrevious()){ nextCalled = false; previousCalled = true; T obj = (T)list[nextIndex--]; return obj; } else { throw new NoSuchElementException(); } }

@Override public int nextIndex() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

@Override public int previousIndex() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

@Override public void remove() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

@Override public void set(T e) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

@Override public void add(T e) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; } private void resize(int newCapacity) { list = Arrays.copyOf(list, newCapacity); capacity = newCapacity; } }

List.java

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: 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