2

如何声明具有 4 行和 3 列的数组。2 列是 int,另一列是 string.help!

        string[, ,] threeD = new string[3, 10, 10];
        threeD[1, 807301, miama]= threeD;
4

6 回答 6

4

我给你一个开始,上课:

class MyItem
{
    public int oneInt {get;set;}
    public int twoInt {get;set;}
    public string oneString {get;set;}    
    public MyItem(int oneInt, int intTwo, string oneString)
    {
      this.oneInt = oneInt;
      this.twoInt = intTwo;
      this.oneString = oneString;

    }
}

然后制作一个列表来容纳 4 个类:

var myFourRowArray = new List<MyItem>();
myFourRowArray.Add(new MyItem(1,252435,"first"));
myFourRowArray.Add(new MyItem(2,235423,"second"));
myFourRowArray.Add(new MyItem(3,454335,"third"));
myFourRowArray.Add(new MyItem(4,346435,"fourth"));
于 2013-05-08T04:33:33.180 回答
2

如果你真的不需要一个数组,你也可以使用DataTable

简单的例子

        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn() { DataType = typeof(int) });
        dt.Columns.Add(new DataColumn() { DataType = typeof(int) });
        dt.Columns.Add(new DataColumn() { DataType = typeof(string) });

        dt.Rows.Add(dt.NewRow());
        dt.Rows.Add(dt.NewRow());
        dt.Rows.Add(dt.NewRow());
        dt.Rows.Add(dt.NewRow());

        int RowId = 0, 
            ColId = 0;

        //Cell access example
        var Cell = dt.Rows[RowId][ColId];
于 2013-05-08T06:26:39.280 回答
2

你可以使用Tuple<T1, T2, T3>这样的数组:

Tuple< int, int, string >[] arr = new Tuple< int, int, string >[4];
于 2013-05-08T04:45:07.493 回答
1

对于具有 4 行和 3 列的多维数组,您可以按以下方式编码:

object[,] _multi = new object[4, 3] { { 1, 2, "jj" }, 
                                      { 3, 4, "jd" },  
                                      { 5, 6, "jz" },  
                                      { 7, 8, "jl" } };
于 2013-05-08T05:33:33.257 回答
0

您可以为此编写自己的自定义数据结构。

例如

public class MyObj
{
    public int column1;
    public int column2;
    public string column3;
    public MyObj(int col1, int col2, string col3)
    {
        column1 = col1;
        column2 = col2;
        column3 = col3;
    }
}

public class MyList
{
    public System.Collections.Generic.List<MyObj> List = new List<MyObj>();
    public void Add(MyObj obj)
    {
        List.Add(obj);
    }
}

public class ProgramTest
{
    public static void Main(string[] args)
    {
        MyList list = new MyList();
        list.Add(new MyObj(1, 807301, "miama"));
        list.Add(new MyObj(1, 807301, "Test2"));
        list.Add(new MyObj(1, 807301, "Test3"));
        list.Add(new MyObj(1, 807301, "Test4"));

        //test
        foreach (MyObj o in list.List)
        {
            Console.WriteLine(o.column1);
        }
    }
}
于 2013-05-08T04:38:37.253 回答
0

数组包含单一类型的项目。

解决这个问题的唯一方法是使用项目类型的共同父类型作为类型。

在这种情况下object

object[,,] threeD = new object[3,10,10];

这将使您在检索项目时投射它们。

作为替代方案,我建议您创建一个包含三个正确类型属性的行的类型,然后创建一个该类型的数组。

不幸的是,这会使处理列变得更加困难,但您可以使用 Linq 来获取列:

var textColumn = list.Select(item.Text);
于 2013-05-08T04:33:17.920 回答