ng80092b
ng80092b

Reputation: 667

Create a empty array of points

I'm trying to create a empty array of points, for this i'm using the following

    Point[] listapontos = new[]; //should create a empty point array for later use but doesn't.
               Point ponto = new Point(); //a no coordinates point for later use
               for (int i = 1; i <= 100; i++) //cycle that would create 100 points, with X and Y coordinates
               {
                   ponto.X = i * 2 + 20;
                   ponto.Y = 20;
                   listapontos[i] = ponto;
               }

I'm having some trouble, because I can't create a empty array of points. I could create a empty array of strings using a list, but since i will need two elements, a list isn't useful here.

Any hints? (hints for the problem are also welcome)

Upvotes: 0

Views: 1273

Answers (3)

Sasha Krassovsky
Sasha Krassovsky

Reputation: 91

The reason this doesn't work is 2-fold. Firstly, when you say

 Point[] listapontos = new[];

you are using invalid syntax. It should be more like

Point[] listapontos = new Point[100];

Now, secondly, when you are writing into the array, you are never creating a new point. Point is a class, which means it is passed by reference. This means that whenever you are writing Ponto's address in, but not a new Ponto.

Instead what you should do is something more like

for(int i = 0; i < 100; i++)
{
    listapontos[i] = new Point(i * 2 + 20, 20);
}

By using the new keyword, you are creating a new point in memory and storing that point's address in the array, instead of writing the address to the same point 100 times into the array.

Upvotes: 1

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

I could create a empty array of strings using a list, but since i will need two elements, a list isn't useful here.

You can define a class like this:

public class Point
{
    public double X {get;set;}
    public double Y {get;set;}
}

Then you can use a List<Point>:

List<Point> points = new List<Point>();  

points.Add(new Point(){X=10, Y=20});

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500765

// should create a empty point array for later use but doesn't.

No, what you've specified just isn't valid syntax. If you want an empty array, you could use any of:

Point[] listapontos = new Point[0];
Point[] listapontos = new Point[] {};
Point[] listapontos = {};

However, then you've got an array with 0 elements, so this statement:

listapontos[i] = ponto;

... will then throw an exception. It sounds like you should either use a List<Point>, or create an array of size 101:

Point[] listapontos = new Point[101];

(Or create an array of size 100, and change the indexes you use - currently you're not assigning anything to index 0.)

It's important to understand that in .NET, an array object doesn't change its size after creation. That's why it's often convenient to use List<T> instead, which wraps an array, "resizing" (by creating a new array and copying values) when it needs to.

Upvotes: 5

Related Questions