0

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)

1
  • @Mathemats: No it's not. ponto is not an array at all in that case... Commented Apr 1, 2015 at 4:22

3 Answers 3

5

// 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.

Sign up to request clarification or add additional context in comments.

1 Comment

So I could create a List of points, just as easily as a strings one?
2

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});

1 Comment

@ng80092b glad to be useful
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.