Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement java collection using MyArrayList Need to fill in lastindexof, e remove, e set public class MyArrayList extends MyAbstractList { public static final int initialCapacity

Implement java collection using MyArrayList Need to fill in lastindexof, e remove, e set public class MyArrayList extends MyAbstractList { public static final int initialCapacity = 1<<3; E[] data = (E[])new Object[initialCapacity]; MyArrayList() { //data = (E[]) new object[block]; size = 0; } MyArrayList(E[] object){ for(int i =0;i = index;i--) { data[i+1] = data[i]; } data[index] = e; size++; } private void ensureCapacity() { if(size>= data.length) { E[] newData = (E[])(new Object[size<<1+1]); System.arraycopy(data, 0, newData, 0, size); data= newData; } } @Override public void clear() { size = 0; data = (E[]) new Object [initialCapacity]; } @Override public boolean contains(E e) { return indexOf(e) != -1; } @Override public E get(int index) { if(index < 0|| index >=size) { throw new IndexOutOfBoundsException("index = " + index); } return data[index]; } @Override public int indexOf(E e) { if(e == null) { for(int i = 0;i < size; i++) { if(e == data[i]) { return i; } } }else { for(int i = 0;i iterator() { return new MyArrayListIterator(); } private class MyArrayListIterator implements java.util.Iterator { private int current = 0; @Override public boolean hasNext() { return(current < size); } @Override public E next() { if(hasNext()) return data[current++]; else return null; } public void remove() { MyArrayList.this.remove(current); } } public static void main(String... strings) { MyArrayList mal = new MyArrayList<>(); mal.add("Chicago"); mal.add(0, "Albany"); System.out.println("After adding two elements, the size becomes " + mal.size()); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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