Question
These are the directions iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the
These are the directions
iii.print out the arraylist iv.reverse all the elements v.print out this arraylist
vi.make a clone of the arraylist
vii.remove all the elements at any odd index of the original arraylist (not the cloned one)
viii.print out the original arraylist
ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order)
xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to visualize it.)
xii.print out the original arraylist
6.Under the main package, in the Main class, remove the contents of the main method we have added in the last lab (or project) and do the following: Lab2.test();
7.Compile and test your implementation.
This is the code I have written that does not work when I try and follow the directions.
First class.
public Class Lab2{
public static void test(String[] cmd){ MyArrayList list = new MyArrayList(); list.append(0); list.append(1); int one = 0,two =1; for(int i=0;i
Second Java class.
package collection;
public class MyArrayList implements Cloneable{
public Object[] obj;
public int maxsize = 100,initialsize = 0;
public MyArrayList(){ obj = new Object[maxsize]; }
public void append(Object element){ if(initialsize
public void clear(){ obj = new Object[maxsize]; }
public void contains(Object element){ int flag = 1; for(int i=0;i public Object elementAt(int index){ return obj[index]; } public int indexOf(Object element){ for(int i=0;i public void insertAt(int index,Object element){ for(int i=initialsize;i>index;i--){ obj[i] = obj[i-1]; } obj[index] = element; } public boolean isEmpty(){ if(initialsize == 0) return true; return false; } public void removedAt(int index){ for(int i=index;i public void remove(Object element){ for(int i=0;i public void replace(int index,Object element){ obj[index] = element; } public int size(){ return initialsize; } public boolean ensureCapacity(int minCapacity){ if(initialsize >= minCapacity) return true; return false; } public Object clone() throws CloneNotSupportedException{ return (MyArrayList) super.clone(); } public void removeRange(int fromIndex,int toIndex){ for(int i=fromIndex;i public String toString(){ String res = " "; for(int i=0;i public void reverse(){ int last = initialsize -1; for(int i=0;i public void merge(MyArrayList arraylist2){ for(int i=0;i }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started