1

So the method remove removes only the first occurrence of an element. I am supposed to Add the method
removeAll() that will remove all occurrences of a given element. I have tried many different things but nothing has worked.

using System;

namespace UnorderedArrayListNamespace

{
    public class UnorderedArrayList
    {
        protected int[] list;
        protected int next;

        public UnorderedArrayList()
        {
            list = new int[100];
            next = 0;

        }

        public void insert(ref int item)
        {
            list[next] = item;
            next++;
        }

        public void remove(ref int item)
        {
            if (next == 0)
            {
            }
            else
            {
            //find value, if it exists
                for (int i = 0; i > next; i++)
                {
                    if (item.Equals(list[i]))
                    {
                        for (int j = i; j < next; j++) list[j] = list[j + 1];
                        next--;
                        break;

                    }

                }
            }

        }

        public void print()
        {
            for (int i = 0; i < next; i++)
            {
               Console.WriteLine(list[i]);
            }
            Console.WriteLine();
        }
    }

}
3
Contributors
4
Replies
13
Views
7 Months
Discussion Span
Last Post by rproffitt
0

So I was given the code there titled UnorderedArrayListNamespace and I am supposed to modify it to add the method RemoveAll() to remove all occurences of an item in the list. Ive tried many things and none of it is working.

0

(You!) Read your code. I see line 31 and a bit more find the item so a rather easy way is for your new method/function to use that to keep calling the old single remove until there are no more found.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.