开发者

Dynamic Array Java program converted to C#

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 Syst开发者_C百科em.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*/

}


Change .length to .Length

Also, change the "data" parameter it's confusing having that being both the member array and a parameter in the same method.


this.data.length

ONE look into the documentation and you will see that the error is right. data is an array. An array does not have a property "length". Not ehte spelling. "length" is not "Length".

Check the documentation of .NET to find the proper spelling or property name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜