Changing Array[][] changes all copies

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an array, result[][]. I populate the array and add it to an ArrayList. I then change result[][] and add the new version to the ArrayList. However, when I go to review the ArrayList, all of the result[][] 's are the same. How can I stop this from happening? I tried copying the array but that didn't work.
 
Eric said:
I have an array, result[][]. I populate the array and add it to an
ArrayList. I then change result[][] and add the new version to the
ArrayList. However, when I go to review the ArrayList, all of the
result[][] 's are the same. How can I stop this from happening? I
tried copying the array but that didn't work.

You're not adding the actual array to the ArrayList - you're adding a
reference. You're then adding the same reference (i.e. a reference to
the same object) later on, just changing the data inside the object.

You need to make a copy of the array, and change the copy and then add
that. You say that didn't work - please post the code.
 
I wrote another little program and tried this with int[][] and it worked. However, it does not work with the following code.

public void addToList( float percent, Cell [][] result)
{
schedList.Items.Add(percent);
Cell [][] temp = new Cell[result.Length][];
for(int x = 0; x < result.Length; x++)
temp[x] = new Cell[result[0].Length];
for(int x = 0; x < result.Length; x++)
for(int y = 0; y < result[0].Length; y++)
temp[x][y] = result[x][y];
list.Add(temp);
}

private void view_Click(object sender, System.EventArgs e)
{
if(schedList.SelectedIndex == -1)
return;
GraphicSchedule gs = new GraphicSchedule((Cell[][])list[schedList.SelectedIndex], db);
gs.Show();
}
 
Eric said:
I wrote another little program and tried this with int[][] and it worked.
However, it does not work with the following code.

public void addToList( float percent, Cell [][] result)
{
schedList.Items.Add(percent);
Cell [][] temp = new Cell[result.Length][];
for(int x = 0; x < result.Length; x++)
temp[x] = new Cell[result[0].Length];
for(int x = 0; x < result.Length; x++)
for(int y = 0; y < result[0].Length; y++)
temp[x][y] = result[x][y];
list.Add(temp);
}

Well, that copies Cell references or values (depending on whether Cell
is a reference type or a value type) - however if Cell is a reference
and you later change the value of the data within those cells, you're
in exactly the same situation again. Perhaps you need to be creating a
copy of the Cell as well?
 
I'm not sure I understand what you mean by making a cope of Cell also. Here is what Cell is.

public class Cell
{
private int maxSize;
private int minSize;
private int currentSize;
private string instrument;
private string skill;
private string day;
private string time;
private string campus;
private ArrayList students;
private string instructor;
private bool isBlank;
private string name;

public string Instructor
{
get
{
return instructor;
}
set
{
instructor = value;
}
}
public int MaxSize
{
get
{
return maxSize;
}
set
{
maxSize = value;
}
}
public int MinSize
{
get
{
return minSize;
}
set
{
minSize = value;
}
}
public int CurrentSize
{
get
{
return students.Count;
}
}
public string Instrument
{
get
{
return instrument;
}
set
{
instrument = value;
}
}
public string Skill
{
get
{
return skill;
}
set
{
skill = value;
}
}
public string Day
{
get
{
return day;
}
set
{
day = value;
}
}
public string Time
{
get
{
return time;
}
set
{
time = value;
}
}
public string Campus
{
get
{
return campus;
}
set
{
campus = value;
}
}
public bool IsEmptyCell
{
get
{
return isBlank;
}
set
{
isBlank = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}


public Cell()
{
name = "empty";
isBlank = true;
}


public void initiate()
{
name = "initialized";
isBlank = false;
students = new ArrayList();
maxSize = 0;
minSize = 0;
currentSize = 0;
instrument = "none";
skill = "";
day = "none";
time = "none";
campus = "none";
instructor = "none";
}

public void reset()
{
name = "initialized";
skill = "";
}

public void addStudent(int ID)
{
students.Add(ID);
currentSize++;
}

public ArrayList getStudents()
{
return students;
}

public void removeStudent(int ID)
{
students.Remove(ID);
currentSize--;
}

public override string ToString()
{
return name;
}

}
 
Eric said:
I'm not sure I understand what you mean by making a cope of Cell also.
Here is what Cell is.

<snip>

When you were changing your array, how were you changing it? Were you
changing some of the data *within* a Cell in the array, or were you
changing the array itself so that it had a reference to a different
Cell completely?
 
Got it, thanks. Thats gonna be a huge amount of processing :-( At least it only has to be ran once per year :-)
 
Back
Top