Solved

Java to C# conversion (Vector class)

Posted on 2006-10-30
2
1,415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 …

737 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