Dynamic Array Java program converted to C#

Posted by Sef on Stack Overflow See other posts from Stack Overflow or by Sef
Published on 2010-04-12T08:49:59Z Indexed on 2010/04/12 8:53 UTC
Read the original article Hit count: 227

Filed under:

Hello,

The folowing program was orignally in java. But i still get 1 error with the program in C#.
(the eror is listed in comment in the 2nd block of code).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynArray
{
public class DynArrayTester
{
    static void Main(string[] args)
    {
        DynArray da = new DynArray(5);

        for (int i = 1; i <= 7; i++)
        {
            da.setData(i, i);
            //da.put(0, 0);
            //da.put(6, 6);
        }
        Console.WriteLine(da);
    }

}/*DynArrayTester*/
}





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 namespace DynArray
{
public class DynArray
{
    //toestand
    private int[] data;

    //gedrag
    public DynArray(int size)
    {
        data = new int[size];
    }

    public int getData(int index)
    {
        return data[index - 1];
    }

    private void expand(int size)
    {

        int[] tmp = data;
        data = new int[size];

        for (int i = 0; i < tmp.Length; i++)
        {
            data[i] = tmp[i];
        }
    }/*expand*/

    public void setData(int index, int data)
    {
        if (0 < index)
        {
            if (index > this.data.length) // ***error, does not contain definition for "lenght" and no exetension method "lenght"***
                expand(index);

            this.data[index - 1] = data;
        }
    }

    public override string ToString()
    {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            buf.Append("[" + i + "]");
            buf.Append(data[i]);
            buf.Append('\n');
        }
        return buf.ToString();
    }

}/*DynArray*/

}

© Stack Overflow or respective owner

Related posts about c#