[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1482
  • Last Modified:

Java to C# conversion (Vector class)

Hello.

Having some problems converting this Java class, using Vector, so seeking some help porting it. The C# equivilent is supposedly ArrayList.

There are some items that I'm unsure about, for example:

The C# ArrayList:
Insert(int index, object value);

but the Java class is using:
insertElementAt(object value, int index);

Convert the class below and the points is yours. Please keep the same structure and names as the class has, eg. from the example above, "object value, int index" (java) and not "int index, object value" (C#).

public class ItemVector
{
    private Vector v;

    public ItemVector()
    {
        v = new Vector();
    }

    public void addElement(Item item)
    {
        if(item != null)
            v.addElement(item);
    }

    public Item elementAt(int i)
        throws Exception
    {
        return (Item)(Item)v.elementAt(i);
    }

    public void removeElementAt(int i)
        throws Exception
    {
        v.removeElementAt(i);
    }

    public void insertElementAt(Item item, int i)
        throws Exception
    {
        v.insertElementAt(item, i);
    }

    public void setElementAt(Item item, int i)
        throws Exception
    {
        v.setElementAt(item, i);
    }

    public boolean removeElement(Item item)
    {
        return v.removeElement(item);
    }

    public Item firstElement()
    {
        return (Item)v.firstElement();
    }

    public int size()
    {
        return v.size();
    }

    public void removeAllElements()
    {
        v.removeAllElements();
    }

    public boolean contains(Item item)
    {
        return v.contains(item);
    }
}

Thanks for reading!
0
valvet
Asked:
valvet
1 Solution
 
gena17Commented:
It's probably should be something like this:

    public class ItemVector
    {
        private ArrayList v;

        public ItemVector()
        {
            v = new ArrayList();
        }

        public void addElement(Item item)
        {
            if (item != null)
                v.Add(item);
        }

        public Item elementAt(int i)
        {
            return (Item)v[i];
        }

        public void removeElementAt(int i)
        {
            v.RemoveAt(i);
        }

        public void insertElementAt(Item item, int i)
        {
            v.Insert(i, item);
        }

        public void setElementAt(Item item, int i)
        {
            v[i] = item;
        }

        public bool removeElement(Item item)
        {
            bool result = v.Contains(item);
            v.Remove(item);
            return result;
        }

        public Item firstElement()
        {
            return (Item)v[0];
        }

        public int size()
        {
            return v.Count;
        }

        public void removeAllElements()
        {
            v.Clear();
        }

        public bool contains(Item item)
        {
            return v.Contains(item);
        }
    }
0
 
valvetAuthor Commented:
I'll work from there, thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now