Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing multi-dimensional array with ArrayList 2

Status
Not open for further replies.

mais123

Programmer
Dec 26, 2007
36
US
Hi, I am using an array to that represent a table in memory:

col1 col2 col3 col4
5 5 10
15 18 23

The app does not know the sie of the array on each execution so I set it to a large size, liek 1000 by 1000(1000 rows thousand columns). Of course it is inneficient as my table might only end up with 10 rows 10 colums and at the end of the program i iterate through 1000.
How can i replace that multi-dim array with ArrayList or another dynamic structure that gorws as u add values to colums and rows?

As far as I understand ArrayList is more of a single-dimension arraym basically array of objects, which only has one row(so hence 1 dimension).

Thanks
 
Hint:
Your ArrayList can contain other ArrayLists. Or maybe an object that describes a row, and contains an ArrayList of rows.

But at that point, you're getting very close to what the DataTable object does, and it's part of the framework, and thus well-tested and works with other parts of the system.

Chip H.



____________________________________________________________________
www.chipholland.com
 
You don't say what version you are using, so thisshows both 2002/2003 and 2005/2008

Don't forget to add: using System.Collections;

Code:
using System;
[b]using System.Collections;[/b]
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		//Any version 
		private ArrayList RowList1;

		private void Button1_Click(object sender, System.EventArgs e)
		{

			RowList1 = new ArrayList();

			for (int r = 1; r <= 10; r++)
			{
				RowList1.Add(new ArrayList());
				for (int c = 1; c <= 10; c++)
				{
					((ArrayList)RowList1[r - 1]).Add(c * r);
				}
			}

		}

		private void Button2_Click(object sender, System.EventArgs e)
		{

			ListBox1.Items.Clear();
			for (int r = 1; r <= 10; r++)
			{
				for (int c = 1; c <= 10; c++)
				{
					ListBox1.Items.Add("Row: " + r.ToString() + ", Column: " + c.ToString() + " = " + ((ArrayList)RowList1[r - 1])[c - 1].ToString());
				}
			}

		}

		//================================== 

		//2005 or greater 

		private List<List<int>> RowList2;

		private void Button3_Click(object sender, System.EventArgs e)
		{

			RowList2 = new List<List<int>>();

			for (int r = 1; r <= 10; r++)
			{
				RowList2.Add(new List<int>());
				for (int c = 1; c <= 10; c++)
				{
					RowList2[r - 1].Add(c * r);
				}
			}

		}

		private void Button4_Click(object sender, System.EventArgs e)
		{

			ListBox2.Items.Clear();
			for (int r = 1; r <= 10; r++)
			{
				for (int c = 1; c <= 10; c++)
				{
					ListBox2.Items.Add("Row: " + r.ToString() + ", Column: " + c.ToString() + " = " + RowList2[r - 1][c - 1].ToString());
				}
			}
		}
	}

}


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top