0

I have an array that contains two values that is supposed to represent a point on a x and y diagram, the first value being the x value of the point and the second value being the y value of the point.

I then want to store all of the points contained in an area of a square in a list.

I put a break point where the array gets added to the list and it seems to be adding the correct values to the list, namely: 100 10 100 9 etc.

But when I run the program it prints 105 for array[0] and 5 for array[1] for each array in the list.

class Program
{
    static void Main(string[] args)
    {
        List<int[]> points = new List<int[]>();

        int[] point = new int[2];

        string topLeftCornerX = "100";
        string topLeftCornerY = "10";

        for (int i = int.Parse(topLeftCornerX); i < int.Parse(topLeftCornerX) +6; i++)
        {
            point[0] = i;
            for (int j = int.Parse(topLeftCornerY); j > int.Parse(topLeftCornerY) -6; j--)
            {
                point[1] = j;
                points.Add(point);
            }
        }

        foreach (int[] item in points)
        {
            Console.WriteLine(item[0]);
            Console.WriteLine(item[1]);
        }

        Console.ReadLine();
    }
}

Is there a problem with the way I'm adding the array to the list or just the way I'm printing the values?

  • You might want to use the `Point` struct to store `X` and `Y` coordinates. – Rufus L Feb 25 '19 at 00:38
  • You keep updating the values of the `point` array, which is always pointing to the same memory location. You should set it to a `new` array in the outer loop, so it refers to a new array (and different location in memory). – Rufus L Feb 25 '19 at 00:43
  • This question was asked before (like [duplciate](https://stackoverflow.com/questions/2156482/why-does-adding-a-new-value-to-list-overwrite-previous-values-in-the-list) I selected) but it may not be obvious to find that yourself. Welcome to SO and thanks for providing very good quality [MCVE] in the post. – Alexei Levenkov Feb 25 '19 at 00:50

2 Answers2

3

You keep updating the values of the point array, which is always pointing to the same memory location. You should set it to a new array in the inner loop, so it refers to a new array (and different location in memory), and then assign the values there.

List<int[]> points = new List<int[]>();            
string topLeftCornerX = "100";
string topLeftCornerY = "10";

for (int i = int.Parse(topLeftCornerX); i < int.Parse(topLeftCornerX) + 6; i++)
{
    for (int j = int.Parse(topLeftCornerY); j > int.Parse(topLeftCornerY) - 6; j--)
    {
        int[] point = new int[2];
        point[0] = i;
        point[1] = j;
        points.Add(point);
    }
}

foreach (int[] item in points)
{
    Console.WriteLine(item[0]);
    Console.WriteLine(item[1]);
}

Console.ReadLine();

That being said, it might make more sense to use the Point structure, which has an X and Y property. This is a little more descriptive and easier to work with than an int[]:

List<Point> points = new List<Point>();            
string topLeftCornerX = "100";
string topLeftCornerY = "10";

for (int i = int.Parse(topLeftCornerX); i < int.Parse(topLeftCornerX) + 6; i++)
{
    for (int j = int.Parse(topLeftCornerY); j > int.Parse(topLeftCornerY) - 6; j--)
    {
        points.Add(new Point(i, j);
    }
}

foreach (Point item in points)
{
    Console.WriteLine($"[{item.X}, {item.Y}]");
}

Console.ReadLine();
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • The interesting note that just changing `int[] point = new int[2];` in the original code to `Point point = new Point();` and keeping the rest the same would fix the code too (due to copy by value semantic). That should properly confuse OP :) (I'm not really sure if that is a good idea to answer "add to list changes last value" again... but your post look useful and complete so may as well upvote) – Alexei Levenkov Feb 25 '19 at 00:59
0

Because here you're modifying the same array (same reference) and add it again to the list

To get this working:

in each cycle in the loop, create new reference of the array point = new int[2];

However, using a struct or a class to represent a point is better.

Amr Aly
  • 26
  • 2
  • 3