如果我想用ArrayList存储一个二维数组要怎么做?

loveqq123 2005-09-23 11:02:10
rt
...全文
2137 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
CoolKin9 2005-09-23
  • 打赏
  • 举报
回复
简单:
int[,] myINT= new int[5,8,12];
ArrayList myList = new ArrayList();
myList.Add(myINT);
GX198302 2005-09-23
  • 打赏
  • 举报
回复
ArrayList能放任何你想放的东西
rcom10002 2005-09-23
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Collections;

namespace ConsoleApp_CS
{
public class Matrix
{

private ArrayList col;
private ArrayList row;
private int colCount;
private int rowCount;

public Matrix()
{
this.col = new ArrayList();
this.row = new ArrayList();
}

public Matrix(int rowCount, int colCount)
{
this.col = new ArrayList();
this.row = new ArrayList();
this.colCount = colCount;
this.rowCount = rowCount;
this.ConstructMatrix(rowCount, colCount);
}

//Set Dimension
public void SetDimension(int rowCount, int colCount)
{
this.ConstructMatrix(rowCount, colCount);
}

//Construct a matrix
private void ConstructMatrix(int rowCount, int colCount)
{
if ((rowCount <= 0) || (colCount <= 0))
{
throw new Exception("Dimension can't be negative");
}

//Change number of rows
if (this.col.Count > rowCount)
{
this.col.RemoveRange(colCount - 1, this.colCount - colCount);
}

//Change number of rows
if (this.col.Count < rowCount)
{
ArrayList append = new ArrayList();
for (int i = this.row.Count; i < rowCount; i++)
{
append.Add(new ArrayList());
}
this.col.AddRange(append);
}

//Set the "row"
this.row.Clear();
this.row = (ArrayList)(this.GetArrayListElement(col.GetRange(0, 1)));

//Todo:Change number of columns
if (this.row.Count > colCount)
{
foreach(object o in this.col)
{
((ArrayList)o).RemoveRange(rowCount - 1, this.rowCount - rowCount);
}
}

//Change number of columns
if (this.row.Count < colCount)
{
ArrayList append = new ArrayList();
for (int i = this.row.Count; i < rowCount; i++)
{
append.Add(null);
}
foreach(object o in this.col)
{
((ArrayList)o).AddRange(append);
}
}

this.colCount = colCount;
this.rowCount = rowCount;
}

//Get the number of rows
int RowCount
{
get
{
return this.colCount;
}
}

//Get the number of columns
int ColumnCount
{
get
{
return this.rowCount;
}
}

//Get a item specified by rowIndex, colIndex
public object GetItem(int rowIndex, int colIndex)
{
if (!RangeCheck(rowIndex, colIndex))
{
throw new ArgumentOutOfRangeException("Parameter rowIndex or colIndex is out of range.");
}

object item;
item = this.GetArrayListElement(this.col.GetRange(rowIndex, 1));
return this.GetArrayListElement(((ArrayList)item).GetRange(colIndex,1));
}

//Set a item specified by rowIndex, colIndex
public void SetItem(object val, int rowIndex, int colIndex)
{
if (!RangeCheck(rowIndex, colIndex))
{
throw new Exception("Parameter rowIndex or colIndex is out of range.");
}

object rowItem;
rowItem = this.col.GetRange(rowIndex, 1);
rowItem = (ArrayList)(this.GetArrayListElement((ArrayList)rowItem));
((ArrayList)rowItem).Insert(colIndex, val);
((ArrayList)rowItem).RemoveAt(colIndex + 1);
}

//Romove a item specified by rowIndex, colIndex
public void RemoveItem(int rowIndex, int colIndex)
{
if (RangeCheck(rowIndex, colIndex))
{
throw new ArgumentOutOfRangeException("Parameter rowIndex or colIndex is out of range.");
}

object item;
item = this.col.GetRange(rowIndex, 1);
((ArrayList)item).Insert(colIndex, null);
((ArrayList)item).RemoveAt(colIndex + 1);
}

//Remove all items in the matrix
public void RemoveAll()
{
this.col.Clear();
}

//Check whether the range is out of that in matrix
private bool RangeCheck(int rowIndex, int colIndex)
{
if ((rowIndex < this.col.Count) && (colIndex < this.row.Count))
return true;
else
return false;
}

//GetValue
private object GetArrayListElement(ArrayList myList)
{
IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
return myEnumerator.Current;
return null;
}
}
}
cancerser 2005-09-23
  • 打赏
  • 举报
回复
我楼上的,你太酷了
sukyboor 2005-09-23
  • 打赏
  • 举报
回复
ArrayList里面放ArrayList就可以了
yellowfish2001 2005-09-23
  • 打赏
  • 举报
回复
struct Data
{
public int ID;
public string Name;
}

ArrayList arr = new ArrayList();

for(int i = 0; i< 100; i++)
{
Data d = new Data();
d.ID = i;
arr.Add(d);
}
lovevsnet 2005-09-23
  • 打赏
  • 举报
回复
直接加就行了
ArrayList.Add(object o);
public class youritem
{
private string Key;
private object Value;
public youritem(string ky,object obj)
{
Key=ky;
Value=obj;
}
...
}
ArrayList a=new ArrayList();
youritem it=new youritem("mike",20);
a.Add(it);
rcom10002 2005-09-23
  • 打赏
  • 举报
回复
帮你写个二维的,等一下,呵呵
amendajing 2005-09-23
  • 打赏
  • 举报
回复
.
sxlfybb 2005-09-23
  • 打赏
  • 举报
回复
using system.collention;
arraylist arr = new arraylist();
arr.add(object);
inshua 2005-09-23
  • 打赏
  • 举报
回复
Dim al[1] As New ArrayList
2021‘someday 2005-09-23
  • 打赏
  • 举报
回复
就是数据结构间的转换吧?
meixiaofeng 2005-09-23
  • 打赏
  • 举报
回复
对象-〉数组
lookfeng 2005-09-23
  • 打赏
  • 举报
回复
关键是看你想这么存!
楼上的也是一种办法。
当然你也可以存储每个元素,自是你自己需要知道规则!

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧