Solved

Java to C# conversion (Vector class)

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Converting Newtonsoft.Json.Linq.JToken to a DateTime 4 66
Need help with a query 6 54
dynamic menu in asp.net c# 11 26
Create XML 5 31
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now