Dumbo
Dumbo

Reputation: 14132

Selecting specific elements of an array in C#

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.

Upvotes: 1

Views: 5809

Answers (7)

Sergey K
Sergey K

Reputation: 4114

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

Upvotes: 0

Antonio Pelleriti
Antonio Pelleriti

Reputation: 859

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

Upvotes: 0

Jethro
Jethro

Reputation: 5916

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

Upvotes: -1

fixagon
fixagon

Reputation: 5566

or IMHO a bit more elegant

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

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

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391734

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

Upvotes: 3

Euphoric
Euphoric

Reputation: 12859

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

Upvotes: 0

Gabriel
Gabriel

Reputation: 1443

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)

Upvotes: 0

Related Questions