Question: I need to stop this from removing odd indices, it is better to start at the end of the list and work backwards, because doing

I need to stop this from removing odd indices, it is better to start at the end of the list and work backwards, because doing it from beginning to end it is updating the elements after the ones it is deleting and pulling them 1 index towards the front, therefore it does not appear every odd index from the original list.

The main class

package main;

import lab.Lab2;

/** * @This program prints a clone of another array * @author jgemerick0 * * @version 2020.20.2 */ public class Main { public static void main(String[] args) throws CloneNotSupportedException { Lab2.test(args); } }

The MyArrayList class

package collection;

public class MyArrayList implements Cloneable {

public Object[] obj; public int maxsize = 800, initialsize = 0;

public MyArrayList() { obj = new Object[maxsize]; }

public void append(Object element) { if (initialsize < maxsize) obj[initialsize++] = element; else System.out.println("Full"); }

public void clear() { obj = new Object[maxsize]; }

public void contains(Object element) { int flag = 0; for (int i = 0; i < initialsize; i++) { if (obj[i] == element) { System.out.println("Found"); flag = 1; break; } } if (flag == 0) { System.out.println("Not Found"); } }

public Object elementAt(int index) { return obj[index]; }

public int indexOf(Object element) { for (int i = 0; i < initialsize; i++) { if (obj[i] == element) return i; } return -1; }

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() { return initialsize == 0; }

public void removedAt(int index) { for (int i = index; i < initialsize - 1; i++) { obj[i] = obj[i + 1]; } initialsize--; }

public void remove(Object element) { for (int i = 0; i < initialsize; i++) { if (obj[i] == element) { removedAt(i); break; } } }

public void replace(int index, Object element) { obj[index] = element; }

public int size() { return initialsize; }

public boolean ensureCapacity(int minCapacity) { return initialsize >= minCapacity; }

public Object clone() throws CloneNotSupportedException { return super.clone(); }

public void removeRange(int fromIndex, int toIndex) { for (int i = fromIndex; i < toIndex; i++) { removedAt(i); } }

public String toString() { StringBuilder res = new StringBuilder(" "); for (int i = 0; i < initialsize; i++) { res.append(obj[i]); } return res.toString(); }

public void reverse() { int last = initialsize - 1; for (int i = 0; i < (initialsize - 1) / 2; i++) { Object temp = obj[i]; obj[i] = obj[last]; obj[last] = temp; last--; } }

public void merge(MyArrayList arraylist2) { for (int i = 0; i < arraylist2.size(); i++) { obj[initialsize++] = arraylist2.elementAt(i); } }

public void print() { for (int i = 0; i < initialsize; i++) { if (i > 0 && (i % 10 == 0)) { System.out.println(); } System.out.print(obj[i] + " "); } } }

The Lab2 class

public class Lab2{ public static void test(String[] cmd) throws CloneNotSupportedException { MyArrayList list = new MyArrayList(); list.append(0); int one = 0,two =1; for(int i=0;i<23;i++){ int third = one + two; list.append(third); one = two; two = third; } System.out.println("ArrayList:"); list.print(); System.out.println(" Reverse the list:"); list.reverse(); list.print(); MyArrayList list2; list2 = (MyArrayList)list.clone(); System.out.println(" ArrayList cloned:"); list2.print(); for(int i=0;i if(i%2!=0) { list.removedAt(i); } } System.out.println(" List after remove:"); list.print(); list2.reverse(); System.out.println(" Reversed cloned list:"); list2.print(); list.merge(list2); System.out.println(" Merged list:"); list.print();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!