问题如下:
有一个二维数组,其长度是不确定的。
如果用ArrayList来定义的话,怎么样对这个二维数组赋值。
就好比:
ArrayList arrays=new ArrayList();
之后不知道怎么样添加数据
比如要:
a[1,0]=1
a[1,1]=2
a[2,0]=11
a[2,1]=22
该怎么样将这些值赋进去呢,以及怎么样取出这些值!谢谢指导!
有一个二维数组,其长度是不确定的。
如果用ArrayList来定义的话,怎么样对这个二维数组赋值。
就好比:
ArrayList arrays=new ArrayList();
之后不知道怎么样添加数据
比如要:
a[1,0]=1
a[1,1]=2
a[2,0]=11
a[2,1]=22
该怎么样将这些值赋进去呢,以及怎么样取出这些值!谢谢指导!
解决方案 »
- C#如何正则获取两个字符串之间的字符串
- [C# Winform Access]按钮触发事件出错。
- 代码怎么控制 ListView(winform)的竖直滚动条?
- 矩阵的运算程序
- 如何"允许服务与桌面交互"
- 请教怎么把WinForm中的listView控件的全部复制到一个richTextBox中?
- 新手求教。
- 对xml实在不懂 50分请教
- 第一个C#应用程序
- "Invalid parameter used" 使用 System.Drawing.Image.FromStream出现这样的错误
- 如何捕捉系统MESSAGE并且直接让他前确定
- 如何让控制台程序在后台运行(没有窗口!!)也就是运行这个程序的时候不打开窗口!
int[] a=new int[2];
a[0]=1;
a[1]=2;
arrays.add(a);
a[0]=11;
a[1]=22;
arrays.add(a);
for(int i=0;i<arrays.count;i++)
{
a=arrays[1];
}
//你就改城arraylist中房一维数组就行了撒
ArrayList arrays = new ArrayList();
for(int i=0;i<10;i++)
{
int[] a = new int[2];
a[0]=1;
a[1]=2;
arrays.Add(a);
} //读的时候
foreach(int[] a in arrays)
{
Console.WriteLine(a[0].ToString());
Console.WriteLine(a[1].ToString());
}运行通过哦
using System.Collections;class Class1
{
[STAThread]
static void Main(string[] args)
{
MyCollection a=new MyCollection(); a[1,0]=1;
a[1,1]=2;
a[2,0]=11;
a[2,1]=22; Print(a); Console.Read();
} private static void Print(MyCollection collection)
{
foreach(int[] itemList in collection)
{
string temp = ""; foreach(int item in itemList)
{
temp += string.Format("{0}\t", item);
} Console.WriteLine(temp);
}
} public class MyCollection : CollectionBase
{
private int InitSize = 1;
public int this[int x, int y]
{
get
{
int[] temp = this[x]; if (y < temp.Length) return temp[y]; int[] newList = new int[y + 1];
Copy(temp, newList); this.List.Remove(temp);
this.List.Insert(x, newList); return newList[y];
}
set
{
int[] temp = this[x]; if (y < temp.Length)
{
temp[y] = value;
} int[] newList = new int[y + 1];
Copy(temp, newList); this.List.Remove(temp);
this.List.Insert(x, newList); newList[y] = value;
}
} public int[] this[int x]
{
get
{
for(int i = x + 1 - this.Count; i > 0; i--)
{
this.List.Add(new int[InitSize]);
} return (int[])this.List[x];
}
} private void Copy(int[] from, int[] to)
{
for(int i = from.Length - 1, j = to.Length - 1, k = 0; i>=0 & j >= 0; i--, j--, k++)
{
to[k] = from[k];
}
} }
}
sealeft(海海)
jackyped(★★★★★★★★★★[我的星星是蓝色-_-!!!])
xvting(xvting) ( )
谢谢三位的解答,综合三位的意见,终于解决了问题!
只有这么一点分了,不好意思!