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);
}
}
//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.");
}
//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.");
}
//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.");
}
//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;
}