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();
        }
    }

}

Recommended Answers

All 4 Replies

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.

What are those many different things? And what did not work?

(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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.