1

I have some difficulties to select certain elements of an array in C#.

Imagine data below:

-3,-3
-2,-2
-1,-1
0,0
1,1
2,2
3,3

They are points of a line in cartesian. Now I store them in a array like this

int[,] linePoints

Now I want to make another array that does not contain first N elements and last M elements of the 'linePoints' array. How it can be done?

So if N is 2 and M is 2 the result array should be:

-1,-1
0,0
1,1

(I do not want to deal with PointF at this step, later the shortened array I can convert to PointF[])

Thanks.

1
  • 1
    Define "first" and "last" when you have a 2D array. Commented Apr 28, 2011 at 11:20

7 Answers 7

3

First of all, I don't think you should drop considering PointF at this point, but let me show you why.

If you have this array:

int[,] linePoints;

And you want to remove the "topmost" N elements, and the "bottommost" M elements, you will need to do some work.

Let me show you the code:

void Main()
{
    int[,] linePoints =
    {
        { -3, -3 },
        { -2, -2 },
        { -1, -1 },
        { 0, 0 },
        { 1, 1 },
        { 2, 2 },
        { 3, 3 },
    };
    int N = 2;
    int M = 2;

    // start of the code you're asking for
    int width = linePoints.GetLength(1);
    int newHeight = linePoints.GetLength(0) - (N + M);
    int[,] newLinePoints = new int[newHeight, width];

    for (int y = 0; y < newHeight; y++)
        for (int x = 0; x < width; x++)
            newLinePoints[y, x] = linePoints[N + y, x];
    // end of the code you're asking for

    linePoints.Dump();
    newLinePoints.Dump();
}

Now, let's see how the above code would look if you had used PointF instead.

void Main()
{
    PointF[] linePoints =
    {
        new PointF(-3, -3),
        new PointF(-2, -2),
        new PointF(-1, -1),
        new PointF(0, 0),
        new PointF(1, 1),
        new PointF(2, 2),
        new PointF(3, 3),
    };
    int N = 2;
    int M = 2;

    // start of the code you're asking for
    PointF[] newLinePoints = linePoints
        .Skip(N)
        .Take(linePoints.Length - (N + M))
        .ToArray();
    // end of the code you're asking for

    linePoints.Dump();
    newLinePoints.Dump();
}

(note: the .Dump() parts there come from the fact that I use LINQPad to test my code.)

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

Comments

0

Are you looking for:

for(int i = n; i<lineOfPoints.Length - m ;i++) line[i]

and you should declare your line something like this:

Piont[] lineOfPoints;

EDIT:
@Rob & Aviad Thanks for pointing it out (lineOfPoints.Length - m)

5 Comments

The terminal condition should be line.Length - m
but he said he doesn't want to iterate to last element , he wants points from m,n interval
if n = 2 and m = 2 (in his example), that loop would print nothing
Is that what he says? He says he doesn't want the first N elements, not that he doesn't want the first elements outside of N..M.
@Rob 10x i guess i haven't drink enough coffee for today
0

First : Use Point or custom structure for points instead. Then you can use :

List<Point> points = // your points here
points = points.Skip(n).Take(points.Count-m-n).ToList();

But if you still want to use array, which I highly discourage mainly because problems like this, then you can use code like this (not tested) :

//int[,] linePoints
int[,] newLinePoints = new int[linePoints.Length-m-n,2] // not sure about order here
for(i = n; i < linePoints.Length - m; i++)
{
  newLinePoints[i-n,0] = linePoints[i,0];
  newLinePoints[i-n,1] = linePoints[i,1];
}

Comments

0

or IMHO a bit more elegant

List<Point> points = new List<Point>()
//add points

List<Point> foundPoints = points.GetRange(n, points.Count - n - m)

Comments

0
int[,] linePoints= new int[,]{ {-3,-3} ,{-2,-2}, {-1,-1}, {0,0}, {1,1}, {2,2}, {3,3} };
 int n=2;
 int m=2;
 for(int i=n;i<linePoints.GetLength(0)-m;i++)
     Console.WriteLine(linePoints[i,0] +","+ linePoints[i,0]);

Comments

0

As I understood you want to get from an array elements skip first n and last m elements and store it in other array you should use this code

 int [,] newpoints = new int[n1,2];
    int j = 0;
    for (int i = n; i < N - m; i++)
    {
        newpoints[j, 0] = linePoints[i, 0];
        newpoints[j, 1] = linePoints[i, 1];
       ++j;
    }

where n1 is equal old array number of row minus n N is number of row in linePoints

     for (int i = 0; i < n1; i++)
        {
            Console.WriteLine("{0},{1}",newpoints[i,0],newpoints[i,1]);
        }

Comments

-1

You could try this.

    List points = new List();

    //Fill you list of points here with points.Add(new Point(2,2)); etc.

    Point p = new Point(2,2);
    if (points.Contains(p))
    {
          points.Remove(p);
    }
    //This will give you a new list where predicate condition is met, where Point X not equal to 2 and point Y is not equal to 2

    var newList = points.Where(p => p.X != 2 & p.Y != 2);
    //Note if you use the above you do not need to remove the point(2,2) from the Points List

2 Comments

It shows him how to work with Arrays in a easy way. Having to loop though the array and remove it or add it to a different array is just extra code where the above gives him an easy solution to remove the point from the array, it also gives him a starting point to work from.
But he's not asking to remove the point with coordinates 2,2, he's asking how to get rid of 2 (N) rows of elements from the top, and 2 (M) rows of elements from the bottom. That's not the same.

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.