Java vs. C#

Indexer


Java
 
//Java doesn't have an indexer feature.
//You have to work around.
class IntIndexer
{
    private String[] myData;

    public IntIndexer(int size)
    {
        myData = new String[size];

        for (int i=0; i < size; i++)
        {
            myData[i] = "empty";
        }
    }

    public String get(int pos)
    {
        return myData[pos];
    }
    public void set(String s, int idx)
    {
       myData[idx] = s;
    }

    public static void main(String[] args)
    {
        int size = 10;

        IntIndexer myInd = new IntIndexer(size);

        myInd.set("Some Value",9);
        myInd.set("Another Value",3);
        myInd.set("Any Value",5);

        System.out.println("\nOutput:\n");

        for (int i=0; i < size; i++)
        {
           System.out.println("myInd["+i+"]: "+myInd.get(i));
        }
    
    }
}

Output:
myInd[0]: empty
myInd[1]: empty
myInd[2]: empty
myInd[3]: Another Value
myInd[4]: empty
myInd[5]: Any Value
myInd[6]: empty
myInd[7]: empty
myInd[8]: empty
myInd[9]: Some Value

//work around for overloaded indexer class OvrIndexer { private String[] myData; private int arrSize; public OvrIndexer(int size) { arrSize = size; myData = new String[size]; for (int i=0; i < size; i++) { myData[i] = "empty"; } } public String get(int pos) { return myData[pos]; } public void set(String value, int pos) { myData[pos] = value; } public int get(String data) { int count = 0; for (int i=0; i < arrSize; i++) { if (myData[i] == data) { count++; } } return count; } public void set(String data, String replace) { for (int i=0; i < arrSize; i++) { if (myData[i] == data) { myData[i] = replace; } } } public static void main(String[] args) { int size = 10; OvrIndexer myInd = new OvrIndexer(size); myInd.set("Some Value",9); myInd.set("Another Value",3); myInd.set("Any Value",5); myInd.set("empty", "no value"); System.out.println("\nOutput: \n"); for (int i=0; i < size; i++) { System.out.println("myInd["+i+"]: " + myInd.get(i)); } System.out.println("\nNumber of \"no value\" entries: "+ myInd.get("no value")); } } Output: myInd[0]: no value myInd[1]: no value myInd[2]: no value myInd[3]: Another Value myInd[4]: no value myInd[5]: Any Value myInd[6]: no value myInd[7]: no value myInd[8]: no value myInd[9]: Some Value Number of "no value" entries: 7

C#
 
using System;
class IntIndexer
{
    private string[] myData;

    public IntIndexer(int size)
    {
        myData = new string[size];

        for (int i=0; i < size; i++)
        {
            myData[i] = "empty";
        }
    }

    public string this[int pos]
    {
        get
       {
            return myData[pos];
        }
        set
       {
            myData[pos] = value;
        }
    }

    static void Main(string[] args)
    {
        int size = 10;

        IntIndexer myInd = new IntIndexer(size);

        myInd[9] = "Some Value";
        myInd[3] = "Another Value";
        myInd[5] = "Any Value";

        Console.WriteLine("\nOutput:\n");

        for (int i=0; i < size; i++)
        {
            Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
        }
    }
}
Output:
myInd[0]: empty
myInd[1]: empty
myInd[2]: empty
myInd[3]: Another Value
myInd[4]: empty
myInd[5]: Any Value
myInd[6]: empty
myInd[7]: empty
myInd[8]: empty
myInd[9]: Some Value

//Overloaded Index using System; class OvrIndexer { private string[] myData; private int arrSize; public OvrIndexer(int size) { arrSize = size; myData = new string[size]; for (int i=0; i < size; i++) { myData[i] = "empty"; } } public string this[int pos] { get { return myData[pos]; } set { myData[pos] = value; } } public string this[string data] { get { int count = 0; for (int i=0; i < arrSize; i++) { if (myData[i] == data) { count++; } } return count.ToString(); } set { for (int i=0; i < arrSize; i++) { if (myData[i] == data) { myData[i] = value; } } } } static void Main(string[] args) { int size = 10; OvrIndexer myInd = new OvrIndexer(size); myInd[9] = "Some Value"; myInd[3] = "Another Value"; myInd[5] = "Any Value"; myInd["empty"] = "no value"; Console.WriteLine("\nOutput:\n"); for (int i=0; i < size; i++) { Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]); } Console.WriteLine("\nNumber of \"no value\" entries: {0}", myInd["no value"]); } } Output: myInd[0]: no value myInd[1]: no value myInd[2]: no value myInd[3]: Another Value myInd[4]: no value myInd[5]: Any Value myInd[6]: no value myInd[7]: no value myInd[8]: no value myInd[9]: Some Value Number of "no value" entries: 7