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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList inside ArrayList clone problem

Status
Not open for further replies.

ajikoe

Programmer
Apr 16, 2004
71
ID
I would like to make a program which can handle ArrayList in ArrayList so it is able to simulate 2D dynamic Array, it is ok but I have a problem when I would like to do shallow copy in my ArrayList inside ArrayList.
Here is my program:
ArrayList AA = new ArrayList();
ArrayList temp = new ArrayList();
temp.Add("hello");
AA.Add((ArrayList)temp.Clone());
Console.WriteLine(((ArrayList)AA[0])[0]);//will show "hello"

ArrayList newAA = new ArrayList();
newAA = (ArrayList)AA.Clone();
Console.WriteLine(((ArrayList)newAA[0])[0]);//will show "hello"

//now change the newAA
((ArrayList)(newAA[0]))[0] = "world";
Console.WriteLine(((ArrayList)newAA[0])[0]);//will show "world"

//AA should not change into "world" also, but it is changed
Console.WriteLine(((ArrayList)AA[0])[0]);//will show "world"

Anyone can help?

Sincerely Yours,
Pujo
 
it is a matter of looking correctly at the problem

imagine it like this

array AA - has an entry (a pointer[/b[) that points to another array (temp). it does not contain the actual array temp, but merely and address to it

array newAA is a clone of AA. what does a clone mean? it means it copies the content of the array AA. what is the content of AA? a pointer to another array. which means that both arrays (AA and newAA) will contain a pointer to the same element

Code:
      ____              ___
     | AA |____________| @ |
     |____|            |___|
                         |
                      ___|___       ___      _______
                     |  temp |_____| @ |____|"world"|
                     |_______|     |___|    |_______|
                         |
      _______           _|_
     | newAA |_________| @ |
     |_______|         |___|

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
You can use a Hashtable and ArrayList to simulate what yow want. Instances of the following class create two dim array of objects.
Code:
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
	internal Hashtable ht;
	public Class1(int lin, int col)
	{
		ht=new Hashtable();
		for (int i=0;i<lin;i++)
		{
			ht.Add(i,new ArrayList(col));
		}
		for(int i=0;i<ht.Count;i++)
		{
			for (int j=0;j<col;j++)
				((ArrayList)ht[i]).Add(null);
		}
	}

	public void SetElem(int i, int j, object obj)
	{
		try
		{
			((ArrayList)ht[i])[j] = obj;
		}	
		catch (Exception ex)
		{
			throw new Exception("Class1::SetElem():Index out of ranges " + ex.GetType() + ":" + ex.Message);
		}
	}
	public object GetElem(int i, int j)
	{
		object objReturn = null;
		try
		{
			objReturn= ((ArrayList)ht[i])[j];
		}
		catch (Exception ex)
		{
			throw new Exception("Class1::GetElem():Index Out of ranges" + ex.GetType()+ ":"  + ex.Message);
		}
		return objReturn;

	}
}
[code]
How to use ?
[code]
Class1 c1= new Class1(2,3);
c1.SetElem(1,2,"george");
c1.SetElem(1,1,"adam");
c1.SetElem(1,0,"smith");
//c1.SetElem(0,3,"morse"); // throws index out of range
string s1= (string)c1.GetElem(1,2);
s1=(string)c1.GetElem(1,3); // throws exception
You can modify the above class to store one type of objects.
Also, the above class could be modified to have ArrayLists with different number of elements e.g. no lines and columns should be specified:
Class1 c1 = new Class1();
c1.GetElem(1,j) where j=1...20
c1.GetElem(2,j) where j=1...30;

-obislavu-



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top