在C#中选择数组的特定元素

时间:2011-04-28 11:13:40

标签: c# arrays select elements

在C#中选择数组的某些元素时遇到一些困难。

想象一下下面的数据:

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

它们是笛卡儿的一条线。现在我将它们存储在像这样的数组中

int[,] linePoints

现在我想创建另一个不包含前n个元素的数组和'linePoints'数组的最后M个元素。怎么做?

因此,如果N为2且M为2,则结果数组应为:

-1,-1
0,0
1,1

(我不想在这一步处理PointF,后来缩短的数组我可以转换为PointF [])

感谢。

7 个答案:

答案 0 :(得分:3)

首先,我不认为你现在应该放弃考虑PointF,但让我告诉你原因。

如果你有这个数组:

int[,] linePoints;

你想要删除“最顶层”的N个元素,以及“最底层的”M元素,你需要做一些工作。

让我告诉你代码:

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

现在,让我们看看如果你使用了PointF,上面的代码会是什么样子。

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

(注意:.Dump()部分来自于我使用LINQPad来测试我的代码这一事实。)

答案 1 :(得分:0)

您在寻找:

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

你应该声明你的行:

Piont[] lineOfPoints;

修改
  @Rob&amp; Aviad感谢您指出(lineOfPoints.Length - m)

答案 2 :(得分:0)

据我所知,你想从一个数组元素中获取跳过前n个和最后m个元素并将其存储在其他数组中 你应该使用这个代码

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

其中n1等于旧的行数减去n N是linePoints中的行数

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

答案 3 :(得分:0)

首先:使用Point或自定义结构代替点。然后你可以使用:

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

但是如果你仍然想使用数组,我强烈反对主要是因为这样的问题,那么你可以使用这样的代码(未经测试):

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

答案 4 :(得分:0)

或恕我直言更优雅

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

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

答案 5 :(得分: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]);

答案 6 :(得分:-1)

你可以试试这个。

    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