473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Changing Array[][] changes all copies

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.
Nov 15 '05 #1
6 1923
Eric <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
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();
}
Nov 15 '05 #3
Eric <an*******@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
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;
}

}
Nov 15 '05 #5
Eric <an*******@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Got it, thanks. Thats gonna be a huge amount of processing :-( At least it only has to be ran once per year :-)
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
4
by: Brad | last post by:
I found an article that discussed dynamic allocation of an array but it seems extremely overloaded in time requirements for function. The process is fine if there's no data in it, but if there is...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
8
by: Kresimir Spes | last post by:
Is the following C code possibile in C# (without the pointers ofcourse) int a; int* b=&a; // or a+100 b=0; what I would like to do is create a new int array object and have it point at the...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
2
by: 4Ankit | last post by:
hey guys i am having trouble changing my array code to include a 'for/ in' structure the code i am trying to change is below: <script type="text/javascript"> var contents = new Array(3)
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.