Solved

Java to C# conversion (Vector class)

Posted on 2006-10-30
2
1,403 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 24
Add different cell to otherwise similiar row 4 45
Hey!!!! 1 25
how to double quote a string for an inline sql statement. 8 73
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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