Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the class ArrayIndexList, a linear list is represented using a one-dimensiona

ID: 3584197 • Letter: I

Question

In the class ArrayIndexList, a linear list is represented using a one-dimensional array element. The data member size is such that the list elements are in positions 0 through size-1 of the array. The method removeNull (which is a method of ArrayIndexList) removes every null element of the list. For example, suppose that the list x is: [1, null, 3, null, null, 6] (list size is 6). Then following the invocation x.removellull(), the list will be [1, 3, 6] (the size is now 3). Write JAVA code for the member method removeNull. (Do not assume the existence of any methods for ArrayIndexList).

Explanation / Answer

/*100% working code*/    Please Rate me first

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayIndexList {
    int size;
    ArrayList<Integer> linearList = new ArrayList<Integer>();

    public static void main(String[] args) {
        ArrayIndexList x = new ArrayIndexList();
        x.linearList.add(1);
        x.linearList.add(null);
        x.linearList.add(3);
        x.linearList.add(null);
        x.linearList.add(null);
        x.linearList.add(6);

        x.size = x.linearList.size();
        System.out.println("Elements of List before removing null elements: ");
        x.printList();

        System.out.println(" Elements of List removing removing null elements: ");
        x.removeNull();
        x.size = x.linearList.size();
        x.printList();
    }

    void removeNull() {
        Iterator<Integer> iList = linearList.iterator();
        while (iList.hasNext()) {
            if (iList.next() == null) {
                iList.remove();
            }
        }

    }

    void printList() {
        for (int i = 0; i < size; i++) {
            System.out.print(linearList.get(i) + " ");
        }
        System.out.println();
        System.out.println("Size is :" + size);
    }

}

Output:

Elements of List before removing null elements:
1 null 3 null null 6
Size is :6

Elements of List removing removing null elements:
1 3 6
Size is :3