Need a little help getting this going and would like to see someones version of
ID: 3622175 • Letter: N
Question
Need a little help getting this going and would like to see someones version of this because mine isn't working.Array List
Use MyArrayList.java as a starting point. It contains the class, constructor, and method
declarations that you will need for this assignment. This class uses Java “generics”. This simply means the type of the objects stored in the list is defined when the list is created. You've seen this before when you have used Java's ArrayList. For example,
ArrayList<Integer> is a list of Integer objects and ArrayList<Book> is a list of
Book objects. MyArrayList has Type used throughout the class definition as a placeholder. When an instance of MyArrayList is created, the type provided will be substituted for the Type placeholder wherever it appears in the class definition. For example, if you create an object with “new MyArrayList<Integer>()”, Java will substitute Integer anywhere Type appears in the class definition.
MyArrayList must use an array internally to store the list. This array will grow based on
the size of the data in the list. When this internal array runs out of room, it should double in size to make more room. Your implementation does not need to shrink the internal array.
Constructors
There must be two constructors:
public MyArrayList()
public MyArrayList(Type [] initial_list)
The first is a no-argument constructor that will simply initialize the object as an empty list.
The second accepts an array and uses it to provide initial values for the list. The constructor
must copy the values from the given list into the object's own internal list.
Size
You must provide a method that returns the number of items in the list.
public int size()
Retrieving an Item
You must provide a method that retrieves the item for a given index.
public Type get(int index)
If the given index is not in the list (either less than zero or greater than the last index in the
list), the get method must throw an IndexOutOfBoundsException.
Setting (Overwriting) an Item
You must provide a method that overwrites the item for a given index.
public void set(int index, Type value)
Like the get method, the set method must also throw an IndexOutOfBoundsException.
Clearing the List
You must provide a method for removing all the items in the list. After calling this method,
the list should be empty.
public void clear()
Adding Items
You must provide a method that allows a new item to be added to the end of the list.
public void add(Type item)
If there isn't enough room in the storage array, you must make a new storage array that is
double the size of the original storage array. You should then copy the values from the old
array into the new array before you dispose of the old array.
Inserting Items
You must provide a method that allows a new item to be inserted at a given index in the list.
public void insert(int index, Type value)
This method should shift all the items from “index” to the end of the list by 1 spot. Then, it
should put the given value into the list at “index”. Like the add method, if there is not
enough room in the array, the storage array should be doubled.
Additionally, the insert method must also throw an IndexOutOfBoundsException
when given an invalid index.
Removing Items
You must provide a method for removing an item at a given index from the list.
public void remove(int index)
The remove method should shift all of items past “index” down by 1 spot to get rid of the
item at “index”. Additionally, the remove method must also throw an
IndexOutOfBoundsException when given an invalid index.
Iterator
You must provide an iterator for visiting all the items in the list. You must have a method
which returns the iterator.
public Iterator<Type> iterator()
You must also implement the iterator as a private inner class.
private class MyArrayListIterator<InnerType>
implements Iterator<InnerType>
Your iterator need only support the hasNext and next methods. It does not need to
support the remove method.
provided code:
import java.util.Iterator;
public class MyArrayList<Type> implements Iterable<Type> {
public MyArrayList() {
}
public MyArrayList(Type [] initial_list) {
}
private class MyArrayListIterator<InnerType> implements Iterator<InnerType> {
public MyArrayListIterator() {
}
public boolean hasNext() {
return false;
}
public InnerType next() {
return null;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public Iterator<Type> iterator() {
return new MyArrayListIterator<Type>();
}
public int size() {
}
public Type get(int index) {
}
public void set(int index, Type value) {
}
public void clear() {
}
public void add(Type item) {
}
public void insert(int index, Type value) {
}
public void remove(int index) {
}
}
Explanation / Answer
import java.util.Iterator;
public class MyArrayList<Type> implements Iterable<Type> {
private Object[] myArray;
private int size;
public MyArrayList() {
myArray = new Object[1];
}
public MyArrayList(Type [] initial_list) {
myArray = new Object[initial_list.length];
for (int i=0; i<initial_list.length; i++) {
myArray[i] = initial_list[i];
}
}
private class MyArrayListIterator<InnerType> implements Iterator<InnerType> {
private int position;
public MyArrayListIterator() {
position=0;
}
public boolean hasNext() {
return position<size;
}
public InnerType next() {
if (position >= size) {
throw new IndexOutOfBoundsException();
}
position++;
return (InnerType) myArray[position-1];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public Iterator<Type> iterator() {
return new MyArrayListIterator<Type>();
}
public int size() {
return size;
}
public Type get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return (Type) myArray[index];
}
public void set(int index, Type value) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
myArray[index] = value;
}
public void clear() {
size=0;
}
public void add(Type item) {
if (size == myArray.length) {
Object[] newArray = new Object[myArray.length*2];
for (int i=0; i<myArray.length; i++) {
newArray[i]=myArray[i];
}
myArray = newArray;
}
myArray[size] = item;
size++;
}
public void insert(int index, Type value) {
if (index<0 || index > size) {
throw new IndexOutOfBoundsException();
}
Object[] resultArray;
if (size == myArray.length) {
resultArray = new Object[myArray.length*2];
for (int i=0; i<index; i++) {
resultArray[i] = myArray[i];
}
} else {
resultArray = myArray;
}
for (int i=size; i>index; i--) {
resultArray[i] = myArray[i-1];
}
myArray = resultArray;
myArray[index]=value;
size++;
}
public void remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
for (int i=index; i<size-1; i++) {
myArray[i] = myArray[i+1];
}
size--;
}
public String toString() {
StringBuffer sb = new StringBuffer("[");
for (int i=0;i<size-1;i++) {
sb.append(String.valueOf(myArray[i]));
sb.append(", ");
}
if (size>0) {
sb.append(String.valueOf(myArray[size-1]));
}
sb.append("]");
return sb.toString();
}
public static void main(String[] args) {
MyArrayList<Integer> a = new MyArrayList<Integer>();
a.add(4);
a.add(5);
a.add(6);
System.out.println(a);
System.out.println("size="+a.size());
a.insert(1,3);
System.out.println(a);
a.insert(0,-1);
System.out.println(a);
a.insert(5,9);
System.out.println(a);
System.out.println("size="+a.size());
a.remove(1);
System.out.println(a);
a.remove(0);
System.out.println(a);
a.remove(3);
System.out.println(a);
System.out.println("size="+a.size());
try {
a.remove(3);
} catch (IndexOutOfBoundsException e) {
System.out.println("Tried to remove nonexistent element.");
}
try {
a.insert(4,3);
} catch (IndexOutOfBoundsException e) {
System.out.println("Tried to insert after the end of the list.");
}
a.set(2,8);
a.set(0,0);
System.out.println(a);
for (Integer x : a) {
System.out.println(x);
}
a.clear();
System.out.println(a);
a.insert(0,7);
System.out.println(a);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.