Solved

Java to C# conversion (Vector class)

Posted on 2006-10-30
2
1,394 Views
Last Modified: 2008-01-16
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
Comment
Question by:valvet
2 Comments
 
LVL 8

Accepted Solution

by:
gena17 earned 500 total points
ID: 17833776
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
 

Author Comment

by:valvet
ID: 17837709
I'll work from there, thank you.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Server Error 11 58
Diagnostics with Net and Net.Sockets 2 29
Where can I ask about my ASP.NET MVC project? 6 36
RegEx with optional part 4 24
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question