如何创建二维动态长度数组? [英] How can I create a 2 dimensional dynamic length array?

查看:23
本文介绍了如何创建二维动态长度数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个二维数组,不知道第一维的大小.

I want to create a 2-dimensional array, without knowing the size of the first dimension.

例如,当我创建一个数组时,我的行数未知.每一行代表一个帐户.每行退出 4 列:ID、名称、用户、密码

For example I have an unknown number of rows, when I create an array. Every row represent an account. Exit 4 columns for every row: ID,name,user,password

我尝试使用锯齿状数组,但不可能有:

I tried with jagged array but it is not possible to have:

int[][] jaggedArray = new int[][3];

我还寻找了 ArrayList、类的实现和一些关于泛型的内容.

Also I looked for ArrayList, implementation with clases and a little about Generics.

我正在寻找一种可以轻松操作数据的解决方案:

I'm searching for a solution that can permit easy manipulation of data as:

  • 添加到列表、选择、输入元素
  • 在数据库查询中使用元素
  • 在其他函数中用作参数

因为我是 .NET (C#) 的新手,请为我提供代码解决方案,而不是通用(寻找)解决方案

Because I'm a newbie in .NET (C#) please provide me with code solutions, instead of generic(look for) solutions

推荐答案

IMO,由于列"是固定的,为此声明一个类:

IMO, since the "columns" are fixed, declare a class for that:

public class Account
{
    public int ID {get;set;}
    public string Name {get;set;}
    public string User {get;set;}
    public string Password {get;set;} // you meant salted hash, right? ;p
}

现在有一个:

List<Account> list = new List<Account>();

这里有你需要的一切:

添加到列表、选择、输入元素

add to list,select,input elements

list.Add

在数据库查询中使用元素在其他函数中作为参数使用

using elements in database queries using as parameters in other functions

没有更多信息的模糊,但您可以传递 Account 或单个值,或整个列表.

vague without more info, but you can pass either the Account or invidual values, or the entire list.

这篇关于如何创建二维动态长度数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆